File management-C Programming

C Programming – File management in C

C supports a number of functions that have the ability to perform basic file operations, which include:
1. Naming a file
2. Opening a file
3. Reading from a file
4. Writing data into a file
5. Closing a file


  • Real life situations involve large volume of data and in such cases, the console oriented I/O operations pose two major problems
  • It becomes cumbersome and time consuming to handle large volumes of data through terminals.
  • The entire data is lost when either the program is terminated or computer is turned off therefore it is necessary to have more flexible approach where data can be stored on the disks and read whenever necessary, without destroying the data. This method employs the concept of files to store data.

File operation functions in C:

Function Name Operation
fopen() Creates a new file for use
Opens a new existing file for use
fclose Closes a file which has been opened for use
getc() Reads a character from a file
putc() Writes a character to a file
fprintf() Writes a set of data values to a file
fscanf() Reads a set of data values from a file
getw() Reads a integer from a file
putw() Writes an integer to the file
fseek() Sets the position to a desired point in the file
ftell() Gives the current position in the file
rewind() Sets the position to the begining of the file

Defining and opening a file:

If we want to store data in a file into the secondary memory, we must specify certain things about the file to the operating system. They include the fielname, data structure, purpose.

 The general format of the function used for opening a file is

 FILE *fp;

The first statement declares the variable fp as a pointer to the data type FILE. As stated earlier, File is a structure that is defined in the I/O Library. The second statement opens the file named filename and assigns an identifier to the FILE type pointer fp. This pointer, which contains all the information about the file, is subsequently used as a communication link between the system and the program.
The second statement also specifies the purpose of opening the file. The mode does this job.

R open the file for read only.
W open the file for writing only.
A open the file for appending data to it.

Consider the following statements:

 FILE *p1, *p2;

 In these statements the p1 and p2 are created and assigned to open the files data and results respectively the file data is opened for reading and result is opened for writing. In case the results file already exists, its contents are deleted and the files are opened as a new file. If data file does not exist error will occur.


Closing a file:

The input output library supports the function to close a file; it is in the following format.

A file must be closed as soon as all operations on it have been completed. This would close the file associated with the file pointer.
Observe the following program.

FILE *p1 *p2;
p1=fopen (“Input”,”w”);
p2=fopen (“Output”,”r”);



The above program opens two files and closes them after all operations on them are completed, once a file is closed its file pointer can be reversed on other file.

The getc and putc functions are analogous to getchar and putchar functions and handle one character at a time. The putc function writes the character contained in character variable c to the file associated with the pointer fp1. ex putc(c,fp1); similarly getc function is used to read a character from a file that has been open in read mode. c=getc(fp2).


The program shown below displays use of a file operations. The data enter through the keyboard and the program writes it. Character by character, to the file input. The end of the data is indicated by entering an EOF character, which is control-z. the file input is closed at this signal.

#include< stdio.h >
file *f1;
printf(“Data input output”);
f1=fopen(“Input”,”w”); /*Open the file Input*/
while((c=getchar())!=EOF) /*get a character from key board*/
putc(c,f1); /*write a character to input*/
fclose(f1); /*close the file input*/
printf(“\nData output\n”);
f1=fopen(“INPUT”,”r”); /*Reopen the file input*/


The getw and putw functions:

These are integer-oriented functions. They are similar to get c and putc functions and are used to read and write integer values. These functions would be usefull when we deal with only integer data. The general forms of getw and putw are:


 /*Example program for using getw and putw functions*/
#include< stdio.h >
FILE *f1,*f2,*f3;
int number I;
printf(“Contents of the data file\n\n”);
for(I=1;I< 30;I++)
while((number=getw(f1))!=EOF)/* Read from data file*/
putw(number,f3);/*Write to even file*/
putw(number,f2);/*write to odd file*/
printf(“\n\nContents of the odd file\n\n”);
printf(“\n\nContents of the even file”);


The fprintf & fscanf functions:

The fprintf and scanf functions are identical to printf and scanf functions except that they work on files. The first argument of theses functions is a file pointer which specifies the file to be used. The general form of fprintf is

 fprintf(fp,”control string”, list);

 Where fp id a file pointer associated with a file that has been opened for writing. The control string is file output specifications list may include variable, constant and string.


 Here name is an array variable of type char and age is an int variable
The general format of fscanf is


 This statement would cause the reading of items in the control string.



 Like scanf, fscanf also returns the number of items that are successfully read.

/*Program to handle mixed data types*/
#include< stdio.h >
FILE *fp;
int num,qty,I;
float price,value;
char item[10],filename[10];
printf(“Input filename”);
printf(“Input inventory data\n\n”0;
printf(“Item namem number price quantity\n”);
for I=1;I< =3;I++)
fclose (fp);
printf(“Item name number price quantity value”);
for(I=1;I< =3;I++)


Random access to files:

Sometimes it is required to access only a particular part of the and not the complete file. This can be accomplished by using the following function:

 1 > fseek


fseek function:

The general format of fseek function is a s follows:

fseek(file pointer,offset, position);

This function is used to move the file position to a desired location within the file. Fileptr
is a pointer to the file concerned. Offset is a number or variable of type long, and position in an integer number. Offset specifies the number of positions (bytes) to be moved from the location specified bt the position. The position can take the 3 values.

 Value Meaning
0 Beginning of the file
1 Current position
2 End of the file.

2 responses to “File management-C Programming

  1. i have 2 text files which contain data as follows:
    hello 2
    hai 3
    come 1
    good 2
    hello 1
    i want the o/p as follows
    hello hai come good
    t1.txt 2 3 1 0
    t2.txt 1 0 0 2
    next time if i enter another file t3.txt with similar data it must get appended to the same file. Pls help. HELP URGENTLY REQUIRED.

Leave a Reply

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

You are commenting using your 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