Signup/Sign In

C Program to Read content of a File and Display it

Below is a simple program to read the content of any file and then print it on the output screen.

#include <stdlib.h>   // for exit() function

int main()
    char c[1000];
    FILE *fptr;

    if ((fptr = fopen("studytonight.txt", "r")) == NULL)
        printf("Error! opening file");
        // exit from program if file pointer returns NULL.

    // read the text until newline 
    fscanf(fptr,"%[^\n]", c);

    printf("Data from the file:\n%s", c);
    return 0;

Data from the file: /* Data in the file will get printed. */