Skip to content

C Tutorial #1 :- File Handling

March 27, 2012

File Handling in C:-


Let us try to create a text file( notepad or .txt  file ):-

Sample.txt

Hello Everyone , Good evening

Here, the above file contains the above data.

Let us try to retrieve the data present in the file using C Program.

#include<stdio.h>

#include<conio.h>

main()

{

FILE *fp;

char ch;

clrscr();

fp = fopen(“sample.txt”,”r”);

while(1)

{

ch=fgetc(fp);

if(ch==EOF)

break;

printf(“%c”,ch);

}

getch();

}

Let us try to understand the above program.

Initially, we have included the header files “stdio.h” and  “conio.h” .

Next,

Consider the function,

Fopen(“FILENAME”,”MODE OF OPERATION”);

Here, we pass the file name and mode of operation to the function.

Basically, three modes of operation are there..

  1. Read mode :- using this, we can only read the data from the file.
  2. Write mode:- using this, we can also write the data, if opened, some existing file, it will lose all previous data.
  3. Append mode:- here, we can append the data to existing file, where we cant do it in write mode.

After taking two parameters, fopen() returns the pointer of type FILE (a structure).

So, we need to declare a pointer of type FILE.

So,

FILE *fp;

Fp = fopen(“sample.txt”,”r”);

After this, fopen places a character pointer pointing to the starting character of the file.

Next, let us understand the functioning of fgetc():-

It does two tasks

  1. Gets the character present at the character pointer and returns it( here in our program, we are storing returned character in variable ch ).
  2. It increments the character pointer by 1 byte or 1 character.

So placed the function in while loop. And printing the ch character value.

And at the end of file , Operating system inserts a value having ASCII value 26 at the End Of File denoted by EOF. So, if ch is equal to EOF, we are breaking the loop.

Let us see another example.

Assume marks.txt having rollno, name and marks:-

1926 Teja  75

1913 Nitish 80

1946 Ranadeep 85

Let us try to retrieve the above data using files.

#include<stdio.h>

#include<conio.h>

main()

{

FILE *fp;

int rollno;

char name[20];

float marks;

fp =fopen(“marks.txt”,”r”);

while(fscanf(fp,”%d %s %f”,&rollno,name,&marks)!=EOF)

{

printf(“\n%d %s %f”,rollno,name,marks);

}

getch();

}

In the above program, we have declared the three variables to store the information from the file.

We have used fscanf() to retrieve the data from the file.

Fscanf scans the file from “fp” pointed file and stores the data into the variables provided.

It will return EOF at the end of file. So we have put the condition in the while loop.

Advertisements

From → C Graphics

4 Comments
  1. barathvuutkuri permalink

    when i am opening a file in append mode,it’s not showing the existing data in the file…
    its showing a blank screen and when i try to enter data into file its going into the main program….

  2. sairam permalink

    bro i had asked u in mouse handling topic

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: