File Handling in C – Set 1

In the last instructional exercise, I enlightened you concerning the essential reassure info and yield capacities.

Today I will enlighten you concerning the second kind of info yield capacities, for example, record info yield capacities. We will examine the idea of the document taking care of in C. So immediately let’s begin.

Record Handling in C

The need for record info yield capacities

During genuine C programming, we frequently need to get to the enormous measure of information. Such information can’t be shown on the screen by once. We can’t store that information into memory because of its enormous size.

Likewise, it is unseemly to store such enormous information in memory, because of its unstable nature. So it is possible that we need to store that information through console again or recover it automatically.

Clearly both the techniques are very wasteful. In such cases, we store information in a record. To controlling those records we use document info yield capacities.

Information association in a record

Information is constantly put away in twofold structure. Anyway, its strategy in putting away that parallel information differs starting with one working framework then onto the next.

Yet, as a developer, we don’t need to make a big deal about that. Compiler merchants carry out this responsibility by composing the best possible library capacities as per focused on working framework.

Activities on File

Given underneath are the tasks brought out on record through C programming.

Making document

Opening a current document

Perusing from a document

Keeping in touch with a record

Moving to a particular area in document

Shutting a document

Presently its opportunity to make a program that will show the substance of a document on the screen.

void main() 
 FILE *fp;
 char ch;
 fp=fopen("demo.txt","r"); //opening file in read mode
 while(ch!=EOF)  //loop will continue till end of file
  ch=fgetc(fp); //reading character from file one by one
 fclose(fp);  //closing the file


Above program will display the contents of the file demo.txt on screen.


In the start of the program I have proclaimed a structure pointer fp. Be that as it may, pause, where is the structure FILE? Well it is as of now characterized in the header document stdio.h. We have to utilize structure variable fp that will point to the present situation in the record.

After that, I have made a character variable ch that will store the character that we read from the record.

Presently I have composed an announcement fp=fopen(“demo.txt”,”r”). fopen() is the capacity which is utilized to open the document. Inside that capacity, I have passed two contentions. One with the name of the document and “r” to indicate that record is opened in reading mode. This capacity completes three errands which are as per the following

It looks for the ideal document on the circle.

After that, it stacks the substance of the record in memory which is called cushion.

At that point, it indicates the structure variable fp the primary character of the document.

Note: Here use of cradle is very significant as it is wasteful to stack each character from the document inevitably. It will likewise require some investment. Support is likewise utilized while composing content in some document. Ensure that the document is now made on this circle.

After that, I have utilized while circling and inside that I have thought of one explanation ch=fgetc(fp) which is utilized to get one character from the record to the variable ch.

On the off chance that the substance of the document comes to an end, at that point estimation of ch will be EOF and the while circle will end.

Toward the finish of the circle, I have utilized printf() capacity to print that character on the screen.

In last I have shut the record utilizing fclose() work.

Record dealing with in C is a significant idea and troublesome as well. Numerous individuals discover trouble in learning this idea.

So I would prescribe you to peruse this instructional exercise appropriately and practice the program. In the event that you have any inquiries, at that point fell allowed to ask it in remark segment.

Leave a Comment

error: Alert: Content is protected!!