File Handling in C

File Handling in C

In this part you will learn: 1. Filling 2. C syntax 3. Showing output In this tutorial I will teach you about the concept of Filing in C. You will learn how to read from a file and how to write in a file. What is Filing? In C language we use filing to keep several records. We build certain programs to write our data in the file and to read our data from the file. The file can be in any format mostly we use txt formats. File Read and Write 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 are the header files that we need for coding. The main header is stdio.h which provides us with most of the functions for coding in C and the second header files is just for using a function which can pause the screen until the user hits any key.
  1. struct time{
  2. int hour;
  3. int min;
  4. int sec;
  5. };
  6. int main()
  7. {
  8. struct time t;
  9. FILE *fptr;
  10. fptr = fopen("time.txt","wb+");
To Read and Write from a file we need a FILE type pointer which will point towards our file. In easy words it will contain the address of our file. In this program we have also made a struct named time which contains three int variables for hours, minutes and seconds. Note that we opened the file in a write mode. Which means we can only write in the file using fptr pointer.
  1. printf("Enter total seconds:");
  2. scanf("%d",&t.sec);
  3.  
  4. t.hour = t.sec / 3600;
  5. t.sec %= 3600;
  6. t.min=t.sec/60;
  7. t.sec %=60;
  8.  
  9. printf("%d:%d:%d\n",t.hour,t.min,t.sec);
Now we asked the user the total number of seconds he wanted to write in the will. Then there is this simple calculation which converts the total seconds into hours and minutes and assign them to the the struct variable.
  1. fprintf(fptr,"Time is: ");
  2. fwrite(&t,sizeof(struct time),1,fptr);
  3. printf("contents written");
  4.  
  5. fclose(fptr);
  6. <\c>
  7.  
  8. Now the first statement prints whatever we want it to print in the file. It is similar to the one we use to print anything on the console only it has a ‘f’ with it which refers to file. Then there is this function called fwrite(). This function takes input the struct which we created as an argument then the size of struct, then the condition which checks if it is true and lastly the pointer which contains the address of the file. It will write everything that was saved in the struct variable in the file. After that we closed the file with the fclosed statement.
  9.  
  10. <c>
  11. printf("\n Now reading ..");
  12. fptr = fopen("time.txt","rb+");
  13. fseek(fptr,ftell(fptr)-sizeof(struct time),SEEK_END);
  14.  
  15. fread(&t,sizeof(struct time),1,fptr);
  16. printf("\n%d:%d:%d\n",t.hour,t.min,t.sec);
  17. fclose(fptr);
  18. }
  19. return 0;
Now we opened the file in the reading mode. In the reading mode we are first pointing our pointer towards our file i.e time.txt and then opening it in read binary plus (rb+) mode because we are using binary file handling. Then we used the fseek() function to set the position of file pointer at the start. Then we used fread() function which reads everything from the file in the struct we sent as an argument. And lastly we print the contents again on the console and closed the file again using fclose(). Execute > compile then Execute > run Output output

Add new comment