Read File Using fopen in C

C program to read the file using fopen function. The fopen function opens a file and returns a pointer to structure FILE, a predefined structure in the “stdio.h” header file. If the file is successfully opened, it returns a reference to the file; if the file cannot be opened, it returns NULL. The fgetc method returns a character from the file, whereas the fclose function shuts it.

#include <stdio.h>
#include <stdlib.h>
int main()
{
char ch, file_name[50];
FILE *fp;
printf("Select the file\n");
gets(file_name);
fp = fopen(file_name, "r");
if (fp == NULL)
{
perror("File is not opening\n");
exit(EXIT_FAILURE);
}
printf("The contents of %s file are\n", file_name);
while((ch = fgetc(fp)) != EOF)
printf("%c", ch);
fclose(fp);
return 0;
}

 

Output:

Inlarn.txt

The content of inlarn.txt files are