File Handling in C: A Comprehensive Guide, Study notes of Computer Communication Systems

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

2025/2026

Available from 09/17/2025

vrushabh-lokhande
vrushabh-lokhande 🇮🇳

7 documents

1 / 11

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
File Handling:
When a program is terminated, the entire data is lost. Storing in a file will preserve
your data even if the program terminates.
If you have to enter a large number of data, it will take a lot of time to enter them all.
However, if you have a file containing all the data, you can easily access the contents
of the file using a few commands in C.
You can easily move your data from one computer to another without any changes.
Types of Files
When dealing with files, there are two types of files you should know about:
Text files
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
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.
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download File Handling in C: A Comprehensive Guide and more Study notes Computer Communication Systems in PDF only on Docsity!

File Handling:

 When a program is terminated, the entire data is lost. Storing in a file will preserve

your data even if the program terminates.

 If you have to enter a large number of data, it will take a lot of time to enter them all.

However, if you have a file containing all the data, you can easily access the contents

of the file using a few commands in C.

 You can easily move your data from one computer to another without any changes.

Types of Files

When dealing with files, there are two types of files you should know about:

Text files

 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

 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.

Working with 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.

Example:

FILE *fptr;

Opening a file - for creation and edit

Opening a file is performed using the fopen() function defined in the stdio.h header file.

Syntax:

ptr = fopen("fileopen","mode");

Closing a File

The file (both text and binary) should be closed after reading/writing. Closing a file is performed using the fclose() function.

Syntax:

fclose(fptr);

Reading a file:

 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.

Example:

#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; }

Write to a file:

 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.

Example:

#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; }