/* cat-1.c - display file lines on terminal * Source: Dave Sinkula. Reading a File Line By Line. * Jan. 12, 2005. DaniWeb. * <http://www.daniweb.com/software-development/c/code/216411> * accessed Jan. 24, 2012. */ #include <stdio.h> int main ( void ) { static const char filename[] = "file.txt"; FILE *file = fopen ( filename, "r" ); if ( file != NULL ) { char line [ 128 ]; /* or other suitable maximum line size */ while ( fgets ( line, sizeof line, file ) != NULL ) /* read a line */ { fputs ( line, stdout ); /* write the line */ } fclose ( file ); } else { perror ( filename ); /* why didn't the file open? */ } return 0; }