C Files

There is plenty of function in C language to handle file Input/Output(I/O)  operations. Here in this article, you will learn to handle I/O operations in C by the use of different functions.

In C programming, a file is a place where we will store the permanent data. A file represents a sequence of bytes which stores a group of related data. The file is a ready-made structure.

Why do we need files?

The following points will explain precisely why we need files for our program.

  • When we store our data in the file, the file will preserve our data even if the program terminates.
  • With the help of files, we can move our data from one computer to another without any changes.
  • In the case of large data, it will take a lot of time to enter them all. However, if you have stored all the data in the file then accessing the contents of the file will be easy using a few commands.

How can we declare files?

While working with files, you have to declare a pointer of file type. This declaration enables communication between the file and the program.

FILE *file_pointer

What are the different functions for file handling?

C Language provides varieties of functions that help in performing basic file operations. All the functions for file handling along with their description are given below.

No.Functionsdescriptions
1fopen()creates a new file or open the existing file
2fclose()closes the file
3getc()reads one character from a file
4putc()writes one character to a file
5fscanf()reads one set of data from a file
6fprintf()writes one set of data to a file
7getw()reads an integer from a file
8putw()writes an integer to a file
9fseek()moves the reading control to the desired point
10ftell()tells the current position in the file
11rewind()moves the control to the starting point

How to open a file?

For opening a file, we will use the function fopen() that will fall under the header file “<stdio.h>”.  We must always open a file before we perform operations like read, write or update on it. The fopen() function is used for opening a file. Below is the syntax of fopen().

Syntax:

file_pointer = fopen(“file_open” , “mode”)
for example,
fopen(“D:\\Cprograms\\firstprogram.txt” , “r”);
fopen(“D:\\Cprograms\\secondprogram.txt” , “wb”);
  • The function will open the file named ‘firstprogram.txt’ for reading as per mode ‘r’.
  • let us consider that the second binary file ‘secondprogram.txt’ does not exist in the location ‘D:\Cprograms’. The second function will create a new binary file named ‘secondprogram.txt’ and opens it for writing mode.

The file mode mentioned in the file will mention the purpose of opening the file. Modes in the file can be of the following types.

Opening Modes in Standard I/O

File ModeDescription of ModeDuring the absence of file
rOpens a text file for reading.fopen() returns NULL when the file does not exist.
rbOpens a binary file for reading in binary mode. fopen() returns NULL when If the file does not exist
wOpens a text file for writing.The contents are overwritten when the file exists. Otherwise, a new file will be created.
wbOpen a binary file for writing in binary mode.The contents are overwritten when the file exists. Otherwise, a new file will be created.
aOpens a text file for append. i.e, Data is added to the end of the file.Creates a new file if the file does not exist.
abOpen a binary file for append in binary mode. i.e, Data is added to the end of the file.Creates a new file if the file does not exist.
r+Opens a text file for both reading and writing.fopen() returns NULL.If the file does not exist.
rb+Opens a binary file for both reading and writing in binary mode.fopen() returns NULL.If the file does not exist.
w+Opens a text file for both reading and writing.Overwrites the file’s contents if it exists. Otherwise creates a new file.
wb+Opens a binary file for both reading and writing in binary mode.Overwrites the file’s contents if it exists. Otherwise creates a new file.
a+Opens a text file for both reading and appending.Creates a new file if the file does not exist.
ab+Opens a binary file for both reading and appending in binary mode.Creates a new file if the file does not exist.

How can we close a file?

The file, whether it may be binary or text must be closed after performing all the operations on it. The function fclose() will close the file which is already opened. See the following syntax which is used for performing fclose() operation.

Syntax:

fclose(file_pointer);

The function ‘fclose()’ will close the file and returns zero when the operation is successful. Otherwise, the function will return EOF, which is a constant defined within the header file ‘stdio.h’.

How can we read and write to the text file?

We will use the function fprintf() and fscanf() for reading and writing to the text file. These functions are same as the printf() and scanf() functions. The only difference that exists between them is that the ‘fprintf()’ and ‘fscanf()’ will expect the pointer to the structure file. They are used for reading and writing to a text file.

Now see the following examples implementing fscanf() and fprintf() functions.

Writing on a file using fprintf() function

#include <stdio.h>
#include <stdlib.h>

int main()
{
   int num;
   FILE *file_pointer;
   file_pointer = fopen("C:\\file.txt","w");

   if(file_pointer == NULL)
   {
      printf("Error incurred!");   
      exit(1);             
   }

   
   fprintf(file_pointer, "Hello! this is my  file by using fprintf...\n");//writing data on the file 
   fclose(file_pointer);//closing the file  

   return 0;
}

The program writes the string that we pass into the file “file.txt”. The fprintf() function will write the set of characters to the file. After compiling and executing this program, you will see a text file ‘file.txt’ which will be created in C drive of your computer. When you open the file named ‘file.txt’ in your C drive you will see the same text that you have written in your program.

Reading a text file using fscanf() function

We will use the fscanf() function for the reading of a set of characters from the file. This function will read a word from the file and returns EOF at the end of the file.

#include <stdio.h>
#include <stdlib.h>

int main()
{
   char buffer[300];
   FILE *file_pointer;

   if ((file_pointer = fopen("C:\\file.txt","r")) == NULL){
       printf("Error! incurred while opening file");

       // Program will exit if the file pointer returns NULL.
       exit(1);
   }

   fscanf(file_pointer,"%s", &buffer);
   printf("The value inside the file is: ");
   printf("%s", buffer);
   fclose(file_pointer); 
  
   return 0;
}

In the above program, as the file ‘file.txt’ is opened in the read mode. So the program will at first read the string present inside the file ‘file.txt’. If the first program of writing inside the file is executed properly then the contents written inside at first will be displayed. When the program is executed,it displays the output as below.

Output:

The value inside the file is:
Hello! this is my file by using fprintf…
SHARE C Files

You may also like...

Leave a Reply

Your email address will not be published.

Share