About Me

Hello there! Before talking about qualifications and professional abilities, I would like to introduce myself. I am a Yuvraj Basnet and professional software developer and have been working in this field for 3 long years. I have finished my graduation from The University of Delhi on Computer Science Engineering I considered myself a reasonably experienced programmer with a particular interest in the human side of software development, computers are fascinating machine, but they're mostly reflection of the people using them. In the art of software development, studying code isn't enough; you have to study the people behind the software, too.

C File Handling

C File Handling

C File Handling

Hi there! In the previous article we discussed Dynamic Memory Allocation
now, In this article, we will learn about file handling in C Programming Language. we will learn to handle standard I/O in C using fscanf(), fprintf(), fread0(), fwrite(), fseek() etc. with the help of illustrations. 

C File:

File may be defined as represents a sequence of bytes on the disk where a group of related data is stored. File is created for permanent storage of data. It is a ready made (blueprint) structure.

Why C Files are needed?

1. When a program is closed, the entire important data is lost. Storing in a file will      protect your data even if the program closed.

2. If you have to input a large number of data, it will take a lots of time to input them all.

3. However, if you have a file containing all the data, you can easily access the contents of the file just give a few commands in C language.

4. You can easily shift your data from one computer to another without any changes.

There are two types of files:

A. Text Files:
  • Text files are the normal .txt files (extension). You can easily make text files using any simple text editors eg. sublime text.
  • When you open those files, you'll see all the contents within the file as simple text. You can easily modify or delete the contents.
  • They take minimum effort to preserve, are easily readable, and provide the least security and it also take bigger storage.
B. Binary Files:
  • Binary files are main the .bin files in your computer.
  • Instead of storing data in simple text, they store it in the binary form such as (0's and 1's).
  • They can contains a higher amount of data, are not readable easily, and provides better security than normal text files.

C File Operations

In C Programming, you can perform four major operations on files (plain text or binary form):

1. Creating a new file
2. Opening an existing file
3. Closing a file
4. Reading from and writing information to a file

1. -When working with files, you need to declare a pointer of type (file). This declaration is   needed for communication between the Programs and files.

2. Opening a Files:

The fopen() function is used to create a new file or to open an existing file which is defined in the stdio.h header files.

General Syntax: ptr = fopen("fileopen","mode");




i. Suppose the file currentprogram.txt doesn't exist in the location D:\cprograms. The first function creates a new file named currentprogram.txt and opens it for writing as per the mode 'w'.
The writing mode allows you to create and modify (overwrite) the contents of the file.

ii. let's suppose the second binary file previousprogram.bin exists in the location D:\cprograms. 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 modify the file.


C File Handling

3. Closing a files:

The files either text and binary should be closed after reading/writing.
fclose function is used to closed the existing files.

General Syntax: fclose(fpt);

Here, fpt is a file pointer associated with the file to be closed.
or fclose() function closes the file and returns zero on success

4. Reading and Writing to a text file:

For reading and writing to a .txt file, we use the functions fprintf() and fscanf() these are the just a file version of the printf() and scanf().

a) Example 1: Write a .txt file

C File Handling

In this program the user input a number and stores in the file program.txt.
After compile and run this program, you can see a text file program.txt created in D drive of your computer. When you open the file, you can see the integer number you entered.

b) Example 2: Read a .txt file

C File Handling

This program reads the integer number present in the program.txt file and print the output on the screen. If you successfully created the file from Example 1, executes this program you will get the integer you entered.
Other functions such as fputc(), fgetchar(), etc. can be used in a same way.

4. Reading and Writing to a Binary file:

For reading and writing to a .bin file, we use the functions fread() and fwrite() these are the just a file version of the read() and write().

a) Writing to a .bin file:

General Syntax: fwrite(addressData, sizeData, numbersData, pointerToFile);

Here, addressData, represents address of data to be written in the disk or memory.
         sizeData, represents size of data to be written in the disk or memory.
         numbersData, represents number of such type of data.
         pointerToFile, represents pointer to the file where you want to write.

Example 3:
C File Handling

In the above program, will create a new file program.bin in the D drive.
and declared a structure Threeintegers with three numbers i.e, num1, num2 and num3, 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 Threeintegers.
Since we are only inserting one part of num, the third parameter is 1. And, the last parameter *fpt points to the file in which we are storing the data.
Finally, the program is closed.

b) Reading from a .bin files:

General Syntax: fread(addressData, sizeData, numbersData, pointerToFile);

The Syntax of reading to a binary file is same as the reading to a text file because it also takes 4 arguments but the only difference is the function used i.e, fread(), fwrite().

Here, addressData, represents address of data to be written in the disk or memory.
         sizeData, represents size of data to be written in the disk or memory.
         numbersData, represents number of such type of data.
         pointerToFile, represents pointer to the file where you want to write.

Example 4:
C File Handling

In this program, wil read the same file program.bin which is stored in D drive and loop through the records one by one.
In simple terms, you read one Threeintegers record of Threeintegers size from the file pointed by *fpt into the structure num. will get the similar records you inserted in above Example(3).

Post a Comment