[prev] [index] [next]

Iterating over Text Files

Character-by-character:

FILE *inf, *outf;
int ch;
while ((ch = getc(inf)) != EOF) { // end-of-file char
	putc(ch, outf);
}

Line-by-line:

FILE *inf, *outf;
char line[BUFSIZE];
while (fgets(line, BUFSIZE, inf) != NULL) {
	puts(line, outf);
}

Assumes inf open for reading, outf open for writing