Detecting end-of-file. - C File

C examples for File:File Read

Description

Detecting end-of-file.

Demo Code

#include <stdlib.h>
#include <stdio.h>

#define BUFSIZE 100//from  w ww .  j a  v  a2s. c om

int main( void )
{
    char buf[BUFSIZE];
    char *filename = "text.txt";
    FILE *fp;


    /* Open the file for reading. */
    if ( (fp = fopen(filename, "r")) == NULL)
    {
        fprintf(stderr, "Error opening file.");
        exit(1);
    }

    /* If end of file not reached, read a line and display it. */

    while ( !feof(fp) )
    {
        fgets(buf, BUFSIZE, fp);
        printf("%s",buf);
    }

    fclose(fp);
    return 0;
}

Result


Related Tutorials