in , ,

C File Handling

C File Handling
C File Handling

C File Handling

In this tutorial, you will find out about file handling in C. You will learn to handle standard I/O in C using fprintf(), fscanf(), fread(), fwrite(), fseek(), and so on with the help of examples.

A file is a compartment in computer storage gadgets used for storing data.


Why files are needed?

• When a program is terminated, the whole data is lost. Putting away in a record will safeguard your information regardless of whether the program ends.

• If you need to enter a large number of data, it will take a ton of effort to enter them all.

However, if you have a file containing all the information, you can without much of a stretch access the substance of the document utilizing a couple of commands in C.

• You can easily move your information starting with one PC then onto the next with no changes.


Types of Files

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

1. Text files

2. Binary files

1. Text files

Text files are normal .txt files. You can without much of a stretch make text documents utilizing any basic word processors, for example, Notepad.

At the point when you open those files, you’ll see all the substances inside the document as plain content. You can undoubtedly alter or erase the substance.

They require the least exertion to keep up, are effectively decipherable, and give the least security and takes bigger storage space.

2. Binary files

Binary files are generally the .bin files on your computer.

Rather than putting away information in plain content, they store it in the paired structure (0’s and 1’s).

They can hold a higher measure of information, are not clear effectively, and gives preferable security over the content files.


File Operations

In C, you can perform four significant procedure on documents, either text or paired:

1. Creating another file

2. Opening a current file

3. Closing a file

4. Reading from and composing information to a file


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.

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.

The syntax for opening a file in standard I/O is:

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

For example,

fopen("E:\\cprogram\\newprogram.txt","w");

fopen("E:\\cprogram\\oldprogram.bin","rb");

Let’s suppose the file newprogram.txt doesn’t exist in the location E:\cprogram. The first function creates a new file named newprogram.txt and opens it for writing as per the mode ‘w’.
The writing mode allows you to create and edit (overwrite) the contents of the file.
Now let’s suppose the second binary file oldprogram.bin exists in the location E:\cprogram. The second function opens the existing file for reading in binary mode ‘rb’.
The reading mode only allows you to read the file, you cannot write into the file.

ModeMeaning of ModeDuring Inexistence of file
rOpen for reading.If the file does not exist, fopen() returns NULL.
rbOpen for reading in binary mode.If the file does not exist, fopen() returns NULL.
wOpen for writing.If the file exists, its contents are overwritten.
If the file does not exist, it will be created.
wbOpen for writing in binary mode.If the file exists, its contents are overwritten.
If the file does not exist, it will be created.
aOpen for append.
Data is added to the end of the file.
If the file does not exist, it will be created.
abOpen for append in binary mode.
Data is added to the end of the file.
If the file does not exist, it will be created.
r+Open for both reading and writing.If the file does not exist, fopen() returns NULL.
rb+Open for both reading and writing in binary mode.If the file does not exist, fopen() returns NULL.
w+Open for both reading and writing.If the file exists, its contents are overwritten.
If the file does not exist, it will be created.
wb+Open for both reading and writing in binary mode.If the file exists, its contents are overwritten.
If the file does not exist, it will be created.
a+Open for both reading and appending.If the file does not exist, it will be created.
ab+Open for both reading and appending in binary mode.If the file does not exist, it will be created.

Closing a File

The file (both text and binary) should be closed after reading/writing.

Closing a file is performed using the fclose() function.

fclose(fptr);

Here, fptr is a file pointer associated with the file to be closed.


Reading and writing to a text file

For reading and writing to a text file, we use the functions fprintf() and fscanf().

They are just the file versions of printf() and scanf(). The only difference is that fprint() and fscanf() expects a pointer to the structure FILE.


Example 1: Write to a text file

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

int main()
{
   int num;
   FILE *fptr;

   // use appropriate location if you are using MacOS or Linux
   fptr = fopen("C:\\program.txt","w");

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

   printf("Enter num: ");
   scanf("%d",&num);

   fprintf(fptr,"%d",num);
   fclose(fptr);

   return 0;
}

This program takes a number from the client and stores in the file program.txt.

After you aggregate and run this program, you can see a text file program.txt made in the C drive of your PC. At the point when you open the document, you can see integer you entered.


Example 2: Read from a text file

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

int main()
{
   int num;
   FILE *fptr;

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

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

   fscanf(fptr,"%d", &num);

   printf("Value of n=%d", num);
   fclose(fptr); 
  
   return 0;
}

This program peruses the whole number present in the program.txt record and prints it onto the screen.

In the event that you effectively made the record from Example 1, running this program will get you the whole number you entered.

Different capacities like fgetchar(), fputc() and so on can be used in a similar way.


Reading and writing to a binary file

Functions fread() and fwrite() are used for reading from and writing to a file on the disk respectively in case of binary files.


Writing to a binary file

To write into a binary file, you need to use the fwrite() function. The functions take four arguments:

address of data to be written in the disk
size of data to be written in the disk
number of such type of data
pointer to the file where you want to write.

fwrite(addressData, sizeData, numbersData, pointerToFile);

Example 3: Write to a binary file using fwrite()

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

struct threeNum
{
   int n1, n2, n3;
};

int main()
{
   int n;
   struct threeNum num;
   FILE *fptr;

   if ((fptr = fopen("C:\\program.bin","wb")) == NULL){
       printf("Error! opening file");

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

   for(n = 1; n < 5; ++n)
   {
      num.n1 = n;
      num.n2 = 5*n;
      num.n3 = 5*n + 1;
      fwrite(&num, sizeof(struct threeNum), 1, fptr); 
   }
   fclose(fptr);

In this program, we create a new file program.bin in the C drive.

We declare a structure threeNum with three numbers – n1, n2 and n3, and define it in the main function as num.

Now, inside the for loop, we store the value into the file using fwrite().

The first parameter takes the address of num and the second parameter takes the size of the structure threeNum.

Since we’re only inserting one instance of num, the third parameter is 1. And, the last parameter *fptr points to the file we’re storing the data.

Finally, we close the file.


Reading from a binary file

Function fread() also take 4 arguments similar to the fwrite() function as above.

fread(addressData, sizeData, numbersData, pointerToFile);

Example 4: Read from a binary file using fread()

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

struct threeNum
{
   int n1, n2, n3;
};

int main()
{
   int n;
   struct threeNum num;
   FILE *fptr;

   if ((fptr = fopen("C:\\program.bin","rb")) == NULL){
       printf("Error! opening file");

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

   for(n = 1; n < 5; ++n)
   {
      fread(&num, sizeof(struct threeNum), 1, fptr); 
      printf("n1: %d\tn2: %d\tn3: %d", num.n1, num.n2, num.n3);
   }
   fclose(fptr); 
  
   return 0;
}

In this program, you read the same file program.bin and loop through the records one by one.

In simple terms, you read one threeNum record of threeNum size from the file pointed by *fptr into the structure num.

You’ll get the same records you inserted in Example 3.


Getting data using fseek()

If you have many records inside a document and need to get to a record at a particular position, you have to circle through all the records before it to get the record.

This will burn through a ton of memory and activity time. A simpler method to get to the necessary information can be accomplished using fseek().

As the name recommends, fseek() looks for the cursor to the given record in the file.


Syntax of fseek()

fseek(FILE * stream, long int offset, int whence);

The first parameter stream is the pointer to the file. The second parameter is the position of the record to be found, and the third parameter specifies the location where the offset starts.

WhenceMeaning
SEEK_SETStarts the offset from the beginning of the file.
SEEK_ENDStarts the offset from the end of the file.
SEEK_CURStarts the offset from the current location of the cursor in the file.

Example 5: fseek()

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

struct threeNum
{
   int n1, n2, n3;
};

int main()
{
   int n;
   struct threeNum num;
   FILE *fptr;

   if ((fptr = fopen("C:\\program.bin","rb")) == NULL){
       printf("Error! opening file");

       // Program exits if the file pointer returns NULL.
       exit(1);
   }
   
   // Moves the cursor to the end of the file
   fseek(fptr, -sizeof(struct threeNum), SEEK_END);

   for(n = 1; n < 5; ++n)
   {
      fread(&num, sizeof(struct threeNum), 1, fptr); 
      printf("n1: %d\tn2: %d\tn3: %d\n", num.n1, num.n2, num.n3);
      fseek(fptr, -2*sizeof(struct threeNum), SEEK_CUR);
   }
   fclose(fptr); 
  
   return 0;
}

This program will begin perusing the records from the file program.bin in the converse request (last to first) and prints it.


Please feel free to give your comment if you face any difficulty here.

salman khan

Written by worldofitech

Leave a Reply

How to Enable the Hidden Screen Recorder in Android 10

How to Enable the Hidden Screen Recorder in Android 10

Best Android Screen-Recording Apps

Best Android Screen-Recording Apps