How to read a file and display its content in C | applied electronics engineering

Latest

How to read a file and display its content in C

By Applied Electronics - Monday, March 13, 2017 No Comments
In this C programming language tutorial we show how to read a file and display its content.

Consider that we have a file called PR1.C in our disk drive. We want to open it, read it's content and display on the screen. The way the file handling works in C is that the C program uses file library functions that interacts with the computer operating system. The computer operating system seeks and finds the file in the disk drive. The functions that does the read and write are available in header file. In C it is the studio.h which stands for standard I/O. This functions are converted by the C compiler and this C compiler also handles the interaction between the C program and the OS. The functions that are available for reading a file is fopen() functions which resides in stdio.h header file.

An example of C program to read a file like PR1.C and display its content is below.

# include "stdio.h"
main( )
{
FILE *fp ;
char ch ;
 
 fp = fopen("PR1.C","r");
 
 while(1)
{ 
 ch = fgetc(fp);
 if (ch == EOF)
 break ;
printf("%c", ch) ;
}
fclose(fp) ;
}

The stdio.h is used because the fopen() function to open and read content of a file resides in it as explained above.

Then down the code you can see two variables of type FILE and char declared. This will be explained later. Next you can see that fopen() function has been used to open the file PR1.C in read mode as indicated by string "r". This function returns address of a structure. The structure used here is called FILE. The fp is a file pointer to the FILE structure and there you can see now why the *fp is used in the variable FILE declaration. The FILE structure is declared in the stdio.h file. When the fopen() statement is executed, it returns the address of structure and saved in the file pointer fp.

Now at this point we have the file PR1.C contain in the form of address. It means we can now access the content of the file. The content of the file is read using the while(1) loop. Each characters in the file PR1.C is read using the fgetc() function. The argument is not the file name PR1.C but the file pointer fp. The while loop keeps on reading characters one by one until the EOF special character is encountered. That is the file PR1.C at the end is terminated by the EOF character which in ASCII is 26. When the program finds this character the while loop breaks and exits out of the indefinite while loop. Also while in the while loop we are displaying the content of the file using the printf statement which should be obvious here. After the while loop terminates we are using the fclose() function to close the file reading operation. This is again declared in the stdio.h file and it has the implementation that the buffer will be emptied.

Tags:

No Comment to " How to read a file and display its content in C "