You are on page 1of 5

C Programming - File management in C

Author: Ramesh B Published on: 31st May 2006 | Last Updated on: 4th Mar 2011

C Programming - File management in C
In this tutorial you will learn about C Programming - File management in C, File operation functions in C, Defining and opening a file, Closing a file, The getw and putw functions, The fprintf & fscanf functions, Random access to files and fseek function. 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 fopen() fclose getc() putc() fprintf() fscanf() getw() putw() fseek() ftell() rewind() Operation Creates a new file for use Opens a new existing file for use Closes a file which has been opened for use Reads a character from a file Writes a character to a file Writes a set of data values to a file Reads a set of data values from a file Reads a integer from a file Writes an integer to the file Sets the position to a desired point in the file Gives the current position in the file 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; fp=fopen(“filename”,”mode”); 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

to the file input. The program shown below displays use of a file operations. p2=fopen (“Output”. *p2. The data enter through the keyboard and the program writes it. similarly getc function is used to read a character from a file that has been open in read mode. The mode does this job.fp1). This would close the file associated with the file pointer. Character by character. Consider the following statements: FILE *p1. R open the file for read only. p1=fopen(“data”.”r”).”w”). 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. The second statement also specifies the purpose of opening the file. The putc function writes the character contained in character variable c to the file associated with the pointer fp1. which is control-z. fclose(p2) The above program opens two files and closes them after all operations on them are completed.h > main() . once a file is closed its file pointer can be reversed on other file. its contents are deleted and the files are opened as a new file. Closing a file: The input output library supports the function to close a file. In case the results file already exists. A file must be closed as soon as all operations on it have been completed. fclose(file_pointer). p1=fopen (“Input”. #include< stdio. ex putc(c. the file input is closed at this signal.”w”). p2=fopen(“results”. If data file does not exist error will occur. Observe the following program. it is in the following format. …. The end of the data is indicated by entering an EOF character.”r”). FILE *p1 *p2. W open the file for writing only. …. … fclose(p1).subsequently used as a communication link between the system and the program. A open the file for appending data to it. The getc and putc functions are analogous to getchar and putchar functions and handle one character at a time. c=getc(fp2).

”W”).fp). The general forms of getw and putw are: putw(integer. f1=fopen(“INPUT”. f2=fopen(“ODD”. } The getw and putw functions: These are integer-oriented functions./*Write to even file*/ else putw(number. putw(number. for(I=1.I< 30. printf(“Data input output”).*f2. /*Reopen the file input*/ while((c=getc(f1))!=EOF) printf(“%c”. /*Example program for using getw and putw functions*/ #include< stdio. f1=fopen(“DATA”.&number).”r”).”w”).”r”).h > main() { FILE *f1.f1). They are similar to get c and putc functions and are used to read and write integer values. /*Open the file Input*/ while((c=getchar())!=EOF) /*get a character from key board*/ putc(c.*f3. fclose(f1).I++) { scanf(“%d”.c).”w”)./*write to odd file*/ .f3). } fclose(f1). /*write a character to input*/ fclose(f1).{ file *f1. f1=fopen(“Input”. int number I. These functions would be use full when we deal with only integer data.f1). /*close the file input*/ printf(“nData outputn”). if(number==-1) break.f2).”w”). f1=fopen(“DATA”. f3=fopen(“EVEN”. while((number=getw(f1))!=EOF)/* Read from data file*/ { if(number%2==0) putw(number. getw(fp). printf(“Contents of the data filenn”).

%s%d%f”.&quantity”).} fclose(f1). } The fprintf & fscanf functions: The fprintf and fscanf functions are identical to printf and scanf functions except that they work on files. while(number=getw(f3))!=EOF) printf(“%d”. 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.filename[10]. printf(“nnContents of the even file”). list). /*Program to handle mixed data types*/ #include< stdio. Here name is an array variable of type char and age is an int variable The general format of fscanf is fscanf(fp. fclose(f2).”control string”. while(number=getw(f2))!=EOF) printf(“%d%d”. constant and string. The control string is file output specifications list may include variable. fclose(f2). Where fp id a file pointer associated with a file that has been opened for writing. Like scanf.item. f2=fopen(“ODD”.5).age. . fclose(f3). printf(“nnContents of the odd filenn”).qty. float price.”5s%d”.h > main() { FILE *fp.number).number). fscanf also returns the number of items that are successfully read.list).”r”).”controlstring”. char item[10]. fprintf(f1.name. f3=fopen(“EVEN”.I. This statement would cause the reading of items in the control string. fclose(f3). Example: fscanf(f2.”r”).7. int num.value.

printf(“Input filename”). position).”%s%d%f%d”. This function is used to move the file position to a desired location within the file.quality).”w”). fprintf(“stdout. for I=1. fp=fopen(filename.&number. scanf(“%s”. Offset is a number or variable of type long.&quality). 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. . fprintf(stdout.I< =3. for(I=1.price. printf(“Item namem number price quantityn”). and position in an integer number. } fclose(fp). printf(“Input inventory datann”0. fp=fopen(filename.I++) { fscanf(fp. value=price*quantity”).&price. fprintf(fp.filename).”nn”).item.number.item.item.I++) { fscanf(stdin.”%s%d%f%d%dn”. } Random access to files: Sometimes it is required to access only a particular part of the and not the complete file.”%s%d%f%d”.offset. Fileptr is a pointer to the file concerned. } fclose (fp).value).quantity. Value Meaning 0 Beginning of the file 1 Current position 2 End of the file.&number.I< =3.price. printf(“Item name number price quantity value”).&quality).”r”).&prince.”%s%d%f%d”. The position can take the 3 values. Offset specifies the number of positions (bytes) to be moved from the location specified bt the position.itemnumber.