In programming, we may require repeatedly generating specific input data. Sometimes it’s not enough to just display the data on the console. The displayed data can be extensive, and only a limited amount of data can be displayed on the console. Since the memory is volatile, it is impossible to continuously restore the software-generated data. However, if we need to do this, we can store it on the local filesystem, which is volatile and available every time. This is where the need to process files in C.
The file handling in C allows us to create, update, read and delete files stored on the local file system with our C program. The following operations can be performed on the file.
There are two types of files to keep in mind when working with files:
Text files are plain .txt files. You can easily create text files using any simple text editor like Notepad.
When you open these files, you will see the entire contents of the file in plain text. You can easily edit or delete the content.
They require minimal maintenance, are easy to read, offer the least security and take up more storage space.
Binary files are basically .bin files on your computer.
Instead of storing data in plain text, they store it as binary (ones and zeros).
It May contain more data, are difficult to read, and are more secure than text files.
There are many functions in the C library to open, read, write, find and close files. The following is a list of file functions:
|1.||fopen()||Open a new or existing file|
|2.||fprintf()||write data to file|
|3.||fscanf()||read data from file|
|4.||fputc()||write one character to file|
|5.||fgetc()||read one character from file|
|6.||fclose()||close the file|
|7.||fseek()||set the file pointer to the specified position|
|8.||fputw()||write an integer to the file|
|9.||fgetw()||read an integer from the file|
|10.||ftell()||return the current position|
|11.||rewind()||Set the file pointer to the beginning of the file|
So far, the operation of the C program has been performed on a command prompt/terminal without storage. But in the software industry, most programs are written to store information extracted from the program. One way is to save the information you get to a file. The different operations that can be performed on files are:
For opening an existing file, fopen function is used with the required access modes. Some of the commonly used file access modes are mentioned below.
File opening modes in C programming language:
- “r” – Search for files. If the file has been successfully opened fopen() load it into memory and set a pointer to the first character in it. If the file cannot be opened, fopen() returns NULL.
- “rb” – Turn on reading in binary mode. If the file does not exist, fopen(); returns NULL.
- “w” – Search for files. If the file exists, its content will be overwritten. If the file does not exist, a new file will be created. If the file cannot be opened, NULL is returned.
- “wb” – Open for writing in binary mode. If the file exists, its content will be overwritten. If the file does not exist, it will be created.
- “a” – Search for files. If the file is successfully opened, fopen() load it into memory and set a pointer to the last character in it. If the file does not exist, a new file will be created. If the file cannot be opened, NULL is returned
- “ab”- Open the attachment in binary mode. The data is added to the end of the file. If the file does not exist, it will be created.
- “r +”- Search for files. If you successfully open it, fopen() loads it into memory and sets a pointer to the first character in it. If the file cannot be opened, NULL is returned.
- “rb +”- Open for reading and writing in binary mode. If the file does not exist, fopen() returns NULL.
- “w +”- Search for files. If the file exists, its content will be overwritten. If the file does not exist, a new file will be created. If the file cannot be opened, NULL is returned.
- “a+” – Search for the file. If the file opens successfully, fopen() loads it into memory and sets a pointer to the last character in it. If the file does not exist, a new file is created. Returns NULL if the file cannot be opened.
- “ab +” – Open for both reading and adding in binary mode. If the file does not exist, it will be created.
As mentioned above, if you want to perform operations on a binary, you must append `b` to the end. For example, instead of “w” you should use “wb”, instead of “a +” you should use “a + b”. To perform operations on the file, a special pointer is used, called the file pointer, which is declared as
file_name – Is a string specifying the name of the file to be opened or created using the fopen method. mode: is a string (usually one character) that specifies the mode in which the file should be opened. There are different ways to open a file in C, we’ll get to all of them later in this article.
When will the file be created?
The fopen function will create a new file when it does not find a file with a specified name in the specified location. Otherwise, if the file is found, it will be opened in the specified mode.
You can use the fscanf or fgets functions to perform file reading operations. These two functions perform the same operations as scanf, but obtain each other through additional parameter file pointers. So it depends on whether you want to read the file line by line or character by character. The code snippet of reading the file is as follows:
File write operations can be performed by fprintf and fputs, as can read procedures. The fragment to be reported to the file looks like this :
Permanently closing the file after each successful operation. To close a file, use the fclose function. The code snippet for completing the file looks like this:
After discussing how to open and close a file, it is important to note that there are 3 types of streams (sequence of bytes) in a file:
- Input \ output
File input / output operations help you read and write to a file.
The simplest functions used when performing operations to read and write characters to a file are getc() and putc(), respectively.
To read and write a data record to a file, we use the fscanf() and fprintf() operators.
The fread() and fwrite() functions are used to read files on the hard disk or write binary files to them.
To write to a binary file, you need to use the fwrite() function. These functions require four parameters:
- The address of the data to be written to the hard disk
- The size of the data to be written to the hard disk
- The number of this type of data
- The pointer to the file to be written
The basic syntax for reading a binary file in C is:
fread(data_address, data_size, number_of_data_items, file_pointer);
The basic syntax for writing in a binary file in C is:
fwrite(data_address, data_size, number_of_data_items,file_pointer);
While they seem to have similar functions, there are some differences between the two.
The writing mode is indicated by “w”, while the adding mode is indicated by “a”.
You may lose data in write mode. The second time you run your program, the system will overwrite the new program output if the user enters something different from the previous program. In contrast to this situation, append mode simply adds more data to the existing data. Therefore, it can be seen from this discussion that adding mode is better than writing mode.
Now that we understand some of the basic operations in text files, let’s move on to our discussion of binary files. We can open and close the binary in a similar way to text files. The difference arises when reading and writing to a binary file.
Occasionally, the output generated by compiling and running the program does not serve this purpose. If we want to repeatedly check the operation of a program, compiling and running the same program multiple times becomes a tedious task. This is where file handling comes into play. Some of the following reasons for the popularity of file processing:
Save time: Some programs require a lot of user action. You can easily access any part of the code with specific commands.
Portability: You can easily transfer the contents of a file from one computer system to another without worrying about data loss.