Example: fgets

The following example, for Windows platforms, opens a file and reads the first line using fgets.

To run the example, copy the file readme.txt from the installation's dat directory to the c drive, or copy another file and change the value of filename.

#include <stdio.h>
    int i, total = 0;
    char line[100], ch;
    FILE* file_stream;
    char *filename = "c:\\readme.txt";
    if ((file_stream = fopen(filename, "r")) == NULL ) {
        lr_error_message("Cannot open %s", filename);
        return -1;
    // Get the first line from the file 
    if (fgets(line, 100, file_stream) == NULL)
        lr_output_message("fgets error");
        lr_output_message( "The first line is \"%s\"", line);
    if (fclose(file_stream))
        lr_error_message("Error closing file %s", filename);
The first line is "**** For an HTML version of the ReadMe file, see readme.htm ****"