






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A comprehensive overview of file handling in the c programming language. It covers essential concepts such as opening, reading, writing, and closing files, along with different file modes and i/o functions. Practical code examples to illustrate each concept, making it easier for learners to understand and implement file handling techniques in their c programs. It also explains the differences between text and binary files, offering a solid foundation for managing data storage and retrieval in c applications. This guide is suitable for students and developers looking to enhance their understanding of file handling in c.
Typology: Study notes
1 / 11
This page cannot be seen from the preview
Don't miss anything!







When dealing with files, there are two types of files you should know about:
Text files are the normal .txt files. You can easily create text files using any simple text editors such as Notepad. When you open those files, you'll see all the contents within the file as plain text. You can easily edit or delete the contents. They take minimum effort to maintain, are easily readable, and provide the least security and takes bigger storage space.
Binary files are mostly the .bin files in your computer. Instead of storing data in plain text, they store it in the binary form (0's and 1's). They can hold a higher amount of data, are not readable easily, and provides better security than text files.
When working with files, you need to declare a pointer of type file. This declaration is needed for communication between the file and the program.
FILE *fptr;
Opening a file is performed using the fopen() function defined in the stdio.h header file.
ptr = fopen("fileopen","mode");
The file (both text and binary) should be closed after reading/writing. Closing a file is performed using the fclose() function.
fclose(fptr);
In order to read a file, we can use fscanf function. This fuction is file version of sacnf function. fscanf expects a file pointer in addition to the other arguments which scanf expects. We will have to open the file in read mode in order to use this function.
#include<stdio.h> int main() { FILE *ptr = NULL; char str[50]; ptr = fopen("vc.txt","r"); fscanf(ptr,"%s",str ); printf("%s",str); fclose(ptr); return 0; }
In order to write to a file, we can use fprintf function. This function is file version of printf function. fprintf expect a file pointer in addition to the other arguments which printf expects. We will have to open the file in write mode in order to use this function.
#include<stdio.h> int main() { FILE *ptr = NULL; char str[50]="Hello, World!"; ptr = fopen("vc.txt","w"); fprintf(ptr,"%s",str ); printf("%s",str); fclose(ptr); return 0; }
fgetc: Example: #include<stdio.h> int main() { FILE *ptr=NULL; ptr=fopen("vc.txt","r"); char c = fgetc(ptr); printf("The character is %c\n",c); fclose(ptr); return 0; } Example: #include<stdio.h> int main() { FILE *ptr=NULL; ptr=fopen("vc.txt","r"); char c = fgetc(ptr); printf("The character is %c\n",c); c = fgetc(ptr); printf("The character is %c\n",c); fclose(ptr); return 0; }
Example: #include<stdio.h> int main() { FILE *ptr=NULL; int i,n; char c; ptr=fopen("vc.txt","r"); n = sizeof(ptr); for(i=0;i<=n;i++) { c=fgetc(ptr); printf("%c",c); } fclose(ptr); return 0; }
fputs: Examples: #include<stdio.h> int main() { FILE *ptr=NULL; ptr=fopen("vc.txt","w"); fputs("Hello, World!",ptr); fclose(ptr); return 0; } r+ : Example: #include<stdio.h> int main() { FILE *ptr=NULL; ptr=fopen("vc.txt","r+"); fputs("World!",ptr); fclose(ptr); return 0; }
w+ : Example: #include<stdio.h> int main() { FILE *ptr=NULL; ptr=fopen("vc.txt","w+"); fputs("World!",ptr); fclose(ptr); return 0; } a+ : Example: #include<stdio.h> int main() { FILE *ptr=NULL; ptr=fopen("vc.txt","a+"); fputs("World!",ptr); fclose(ptr); return 0; }