File Handling Functions in C
File Handling Functions in C
In programming, we can need a certain type of input data to be generated multiple times. Sometimes it's not enough to simply show the data on the console. It is challenging to repeatedly restore the programmatically created data since the memory is volatile.
The amount of data that can be displayed on the console may be extremely tiny or incredibly large. However, if necessary, we may always access and save data on the local file system, which is volatile. In this case, file management in C is required.
Why is C's File Handling module required?
As of now, the C program's activities are carried out on a prompt or terminal that is not saved anywhere. If the program closed, the output is erased. However, in the software industry, the majority of applications are created to store the data that they get. The circumstance definitely begs for the use of file handling.
Let's examine a few characteristics of using files in order to comprehend why file management is crucial:
Reusability:
The file's data can be viewed, changed, and deleted at anytime, anywhere, offering excellent reusability.
Portability:
Because of portability, files in the computer system can be moved from one place to another without losing any data. With this functionality, the possibility of faulty coding is reduced.
Efficient:
For some programs, a significant quantity of input may be necessary. File handling makes it simple to access a specific section of a file with just a few instructions, which saves time and lowers the possibility of mistakes.
Storage Capacity:
You can store a lot of data in files without having to worry about fitting it all into a program at once.
File Operations:
We can use our C application to create, update, read, and delete files that are stored on the local file system thanks to file handling in C. A file can undergo the following procedures.
- Create a new file.
- Accessing an already open file
- Reading data from a file.
- Adding data to the file.
- Erasing the document.
File handling in C refers to the actions of creating, opening, reading, writing, and closing a file.
File Handling Functions:
The C programming language offers a variety of functions, including fopen(), fwrite(), fread(), fseek(), and fprintf(), that can be used in our application to conduct input, output, and a variety of other C file actions.
fopen():
The C library function fopen() is used to open a file for a variety of actions, such as reading, writing, and other operations, as well as different modes. If the file already exists, it is opened; otherwise, a new file is created.
fclose():
The fclose() function of the C library closes the memory stream that the fopen() function has opened. The fclose() function of the C programme is demonstrated in this session.
fwrite():
The stdio.h header file contains the function fwrite(), which is a part of the C standard library. We can use it to write data into files, both text and binary files in most cases.
fread():
The fread() function is the fwrite() function's counterpart. To read binary data, people frequently utilize the fread() function. The same arguments as the fwrite() function are also accepted.
fprintf():
The C fprintf() method sends inputs to the file indicated by the stream in the format given. For writing prepared data in any file, this function is implemented in applications that deal with files. This article shows you how to utilize the C program's fprintf() function.
fscanf():
A file's formatted input is read using the fscanf() function in C. Programs that deal with files implement this function to read formatted data from any specified file. This article shows you how to use the C program's fscanf() function.
fseek():
The file pointer is set to the provided offset using the fseek() method. Using it, data can be written into a file at a specific location.
fputc():
One character can be written into a file using the fputc() method. A character is output to a stream.
fputc():
The file's first character is returned by the fputc() function. The character comes from the stream. At the end of the file, it returns EOF.
Sample program using file handling functions:
#include<stdio.h>
void main(){
FILE *file;
int i,n;
char data[100],readData[100];
// 1. Opening a file using fopen()
file=fopen("example.txt","w");
if (file==NULL){
printf("File could not be opened.\n");
return 1;
}
// 2. Writing data to the file using fprintf() and fwrite()
fprintf(file, "This is a sample file.\n");
fwrite("Hello, World!", sizeof(char), 14, file);
// 3. Closing the file using fclose()
fclose(file);
// 4. Reopening the file in read mode using fopen()
file=fopen("example.txt","r");
if(file == NULL){
printf("File cannot not be opened.\n");
return 1;
}
// 5. Reading data from the file using fread() and fgetc()
fread(readData, sizeof(char), 20, file);
printf("Read using fread(): %s\n", readData);
i=0;
while((n=fgetc(file))!=EOF){
data[i]=n;
i++;
}
data[i]='\0';
printf("Read using fgetc(): %s\n", data);
// 6. Resetting the file position indicator using fseek()
fseek(file,0,SEEK_SET);
// 7. Writing data to the file using fputc()
fputc('!', file);
// 8. Reading a single line from the file using fgets()
fgets(readData, sizeof(readData), file);
printf("Read using fgets(): %s\n", readData);
// 9. Reopening the file in write mode using fopen()
file= fopen("example.txt","w");
if (file==NULL){
printf("File could not be opened.\n");
return 1;
}
// 10. Reading and writing formatted data using fscanf()
fprintf(file, "123\n");
fscanf(file, "%d", &n);
printf("Read using fscanf(): %d\n", n);
// Closing the file using fclose()
fclose(file);
}
Output:
The output for the given code will depend on the contents of the "example.txt" file. Here's an example of the possible output:
Note: that the output may vary depending on the system and the content of the file.