/* Test program file1 This example demonstrates how to read a simple text file. The text file can be created / edited with any text editor or with test program file2. Author: PS-Trainer 2001.12.01 */ // fopen( stream, "rt" ) // open, readonly, text // fgets(buffer,charmax,stream) // read string until CR/LF // ch = getchar(stream) // read character // fclose( stream ) // close #include #include void main( void ) { FILE *stream; int iok, charmax,linemax,zeile; char line[100]; char infile[]="datain.txt"; iok=zeile=0; charmax=100; // max chars in a single line linemax=100; // max lines in document if( (stream = fopen( infile, "rt" )) != NULL ) { printf("> File %s opened\n",infile); iok++; while ( (fgets(line,charmax,stream) != NULL) && (zeile<=linemax)) { printf( ">%3d: %s", zeile++,line); } fclose( stream ); printf("\n> File %s was closed\n",infile); } else printf("> Error: File %s could not open\n",infile); // show console window until key pressed printf("\nType any key to exit this program.\n"); _getch(); }