Filing in C

Reading From a File

In this part you will learn: 1. C syntax 2. Filing 3. Reading from a file In this tutorial we will learn about filing in C. Filing is a very important tool in C. In this program we will read a file from our computer and display its content on the screen. We will ask the user the name of the file and then we will fetch data from that file and store it somewhere in the program to later display it on the screen. Reading from a File Basic Step: Open Dev C++ then File > new > source file and start writing the code below.
  1. #include<stdio.h>
  2. #include<conio.h>
These two are the most common and basic header files. The first one stdio.h is used to grant us most of the basic functions of C like Input and Output functions. The second one conio.h provides us functions like getch() which is used to pause our screen until we press a button.
  1. int main()
  2. {
  3.         char ch, filename[25];
  4.  
  5.         printf("Enter the name of the file: ");
  6.         scanf("%s",&filename);
  7.  
  8.         FILE *fp;
  9.         fp=fopen(filename,"r");
We first declared a character array of size 25 to store the name of the file. Then we took input from the user in that array. After that wo declared FILE pointer and assigned it the address of our file. Note here the syntax for opening a particular file. This is the fopen function which opens a certain file whose name we enter as the first parameter. There is the ‘r’ letter passed in the parameter of fopen function also. This r means that we are opening our file in read format. That now we can only read from a file by not write anything.
  1. printf("The matrix you entered\n");
  2.         int k,l;
  3.         for( k=0;k<rows;k++)
  4.         {
  5.                 for( l=0;l<columns;l++)
  6.                 {
  7.                         printf("%d",matrix[k][l]);
  8.                         printf(" ");
  9.                 }
  10.                 printf("\n");
  11.  
  12.         }
Now after taking input all the elements of the matrix we are printing them using the similar nested for loops but this time we are printing the value of the elements columnwise.
  1. if(fp==NULL)
  2.         {
  3.                 printf("\nThe file cannot be found.");
  4.         }
  5.         else
  6.         {
  7.                 printf("\nFile contents are:\n");
  8.                 while( ( ch = fgetc(fp) ) != EOF )
  9.                 printf("%c",ch);
  10.                
  11.         }
  12.        
  13.         fclose(fp);
  14.        
  15.  
  16.         getch();
  17.        
  18. }
In this part just after opening the file we placed a condition that if the name of the file that we entered does not corresponds to any file in our folder where we are saving our program then it should output that no file is found and the program should end. Otherwise if the file exists we place a while loop that fetch a character from the file, place it in a character variable that was declared by us. Then in the body of that loop that character is printed. The loop runs until the end of file is reached. When the end of file is reached the condition is falsified and the program breaks out of the loop. At the end we close the file by writing the fclose() function and pauses the program with getch() function. Execute > compile then Execute > run Output picture viewer picture viwer

Add new comment