See the Tutorial List

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<stdio.h>
#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.
        exit(1);         
    }

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

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

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