File Handling in C

File Handling in C



What is File :

The file is a permanent storage where a user can store data and information permanently. Files are used to read and access data from hard disk.

Why we need files :

1. User can transfer any data or information from one device to another device easily using files.
2. When user terminate a program, whole data is lost. But when user store that data or information in a file, it will be reserved under hard disk.

Types of files :

1. Text files
2. Binary files

1. Text files :

Text file contains data in the form of alphabets, digits and special characters by storing their ASCII values. That means in text file a user can store data in simple text format.

How to create a Text file :
User can create text file using .txt extension.

Example :
eruditors.txt
Notes about Text files :
1. In text file data or informations are directly readable.
2. Users don't require any dedicated software or applications to access text files.

2. Binary files :

Binary file contains data in the form of sequence of bytes. In binary file a user can store multiple types of data like image, audio, text etc under a single file. Binary files store data in 0 1 format, means in binary format.

How to create a Binary file :
User can create binary file using .exe, .doc, .mp3, .bin extensions.

Example :
eruditors.doc

Notes about Binary files :
1. Binary files are secure rather than text files.
2. In binary file data or informations are not directly readable.
3. Users require dedicated software or applications to access binary files.

What is File I/O :

File I/O represents input output operation ; like opening, closing, reading ,writing etc.

File Operation :

In C programming language lots of file functions are included in the header file “stdio.h” to execute different types of operations like : for opening a file, for closing a file, for writing a file etc. and to operate those functions we need some of modes like read(r) , write(w), append(a) etc.

Point to be Noted :
~ We need to declear a pointer of type FILE; like FILE *ptr

Opening a file :

To open any file “fopen()” function will be used.
Syntax :
ptr =fopen(“file_name”,"mode");
Example :
1. fopen(“eruditors.txt”,"r");
2. fopen(“eruditors.txt”,"w");
3. fopen(“eruditors.txt”,"a");

Closing a file :

When users open any file they have to close that file using “fclose()” function.
Syntax :
fclose(fptr);

Reading a file :

When users want to read any file then they used “fscanf()” function. fscanf function is nothing but a file version of scanf function. User needs to open file in read mode to use this function.
Syntax :
fscanf (ptr,”%s”,string_name);

Program :

Write a C program to read file content using fscanf function.

Solution :
Here we need to create a text file first, here we take a text file named “eruditors.txt” and write some content under this text file.

Code :
#include<stdio.h>
int main()
{
FILE *ptr = NULL;
char string[47];
ptr = fopen("eruditors.txt","r");
fscanf(ptr,"%s",string);
printf("here the content of this dedicated file :%s\n",string);
return 0;
}

Output :
here the content of this dedicated file : Technology

Point to be Noted :
1. fscanf always expect a file pointer(ptr).
2. Here char string can hold highest number of character 47 only for this dedicated example. You can increase or decrease it’s size.
3. fscanf not counting space means if you write “Technology is good” under your text file; it prints only “Technology”. But if you write “Technology_is_good” under your text file; it prints “Technology_is_good” as it is.

Writing a file :

When users want to write something in a file then they can use fprintf() function. fprintf() function is nothing but a file version of printf function. User needs to open file in write mode to use this function.

Syntax :
fprintf(ptr,”%s”,string_name);

Program : 

Write a C program to write file content using fprintf function.

Solution :
Here we need to create a text file frist, we take a text file named “eruditors.txt” and write some content under this text file. Now we'll replace file content using fprintf function.

Code :

#include<stdio.h>
int main()
{
 FILE *ptr = NULL;
 char string[47] = “Eruditors Technology”;
 ptr = fopen("eruditors.txt","w");
 fprintf(ptr,"%s",string);
 return 0;
}

Output :
Eruditors Technology

Point to be Noted :

1. fprintf always expect a file pointer(ptr).
2. Here char string can hold highest number of character 47 only for this dedicated example. You can increase or decrease it’s size.
3. Suppose content of our dedicated text file “eruditors.txt” is “Programmer”. Now if we use fprintf function and open it in write mode(w) fprintf function will replace “Programmer” into “Eruditors Technology”;

Other function :

~ fputc() : This function is used to write a character into a file.

~ fgetc() : This function is used to read a character into a file.

~ fputw() : This function is used to write a integer into a file.

~ fgetw() : This function is used to read a integer into a file.

~ fseek() : This function is used to set the file pointer in a given position.

~ rewind() : This function is used to set the file pointer at the beginning of the file.

Mode :

Mode is another argument of fopen function. Mode tells your fopen function what you want to do with your dedicated file.

For text file :

"r" : When users try to open a text file using fopen function for reading purpose, then they used “r” mode.

Example :
fopen(“eruditors.txt”,”r”);

Point to be Noted :
“r” : mode means “read mode”.
"w" : When users try to write something under a text file using fopen function then they used “w” mode. If that named file does not exist then a new file is created.

Example :
fopen(“eruditors.txt”,"w");

Point to be Noted :
1. “w” mode remove all existing content of that dedicated file and replaced new content.
2. “w” mode means “write mode”.

~ “a” : When users want to add existing content with new content in a text file, then they used “a” mode. If that named file does not exist then a new file is created.

Example :
fopen(“eruditors.txt”,”a”);

Point to be Noted :
1. “a” mode means “append mode”.
2. “a” mode add new content at the end of existing content.

Example : 
If your existing content is “Eruditors” and you want to add “ is a programmer”. Then “a” mode works like; “Eruditors is a programmer”.

Some other modes :

~ “r+” : When users want to open a text file for both read and write purpose then they used “r+” mode.

~ “w+” : This mode also used for read and write in a text file. This mode replaced all old content with new content. This mode also create a new text file if that named file does not exist.

~ “a+” : This mode also used for read and write in a text file. When users want to read and add existing content with new content, then they used “a+” mode. If that named file does not exist then a new file is created.

For binary file :

~ “rb” : When users try to open a binary file using fopen function for reading purpose, then they used “rb” mode.

~ "wb" : When users try to write something under a binary file using fopen function then they used “wb” mode.

~ “ab” : When users want to use append mode in a binary file, then they used “ab” mode.

~ “rb+” : When users want to open a binary file for both read and write purpose then they used “rb+” mode.

~ “wb+” : This mode also used for read and write in a binary file. This mode replaced all old content with new content.

~ “ab+” : This mode also used for read and write in a binary file. When users want to read and add existing content with new content, then they used “ab+” mode.