I am stuck on this. I am currently using:
FILE *a = fopen("sample.txt", "r"); int n; while ((n = fgetc(a)) != EOF) { putchar(n); }
However, this method seems a bit inefficient. Is there a better way? I tried using fgets:
char *s; fgets(s, 600, a); puts(s);
One thing that I think is wrong with respect to this second method is that for the second argument to fgets you will need a really large amount.
Thanks for all the suggestions. I found a way (someone from the IRC told me this) using open (), read () and write ().
char *filename = "sample.txt"; char buf[8192]; int r = -1; int in = open(filename, O_RDONLY), out = 0; if (in == -1) return -1; while (1) { r = read(in, buf, sizeof(buf)); if (r == -1 || r == 0) { break; } r = write(out, buf, r); if (r == -1 || r == 0) { break; } }
source share