How to Read a File in C
In this tutorial, we are going to see how to read a file in C and display its content on the screen. fopen function is used to open a file. It returns a pointer to the FILE structure which is a predefined structure in the header file stdio.h. If the file is opened successfully, the fopen function returns a pointer to the file and if it fails to open the file, it returns NULL. The fgetc function returns the character read from the file and the fclose function closes the file. The file to be read must be present in the directory where the executable file of this program is located.
How to Read a File in C
#include <stdio.h> #include <stdlib.h> int main() { FILE *f; char c; f=fopen("test.txt","rt"); while((c=fgetc(f))!=EOF){ printf("%c",c); } fclose(f); return 0; }
Output:
[st_adsense]