« Previous                                                                  Next Chapter »
Data Files

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

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 beginning of the file

 

C supports a number of functions that have the ability to perform basic file operations, which include:

1. Open & Close files

2. Read from & Write to files

3. Delete files

Commands for all of the above are found in the stdio.h header file.

1. Opening Files

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 file name, data structure, and purpose. The general format of the function used for opening a file is

Syntax:  

               FILE * fopen(char * filename, char * mode);

                             (or)

               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 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.

Types of modes

r - Open the file for read only

w - Write to the file, overwriting existing data

a - Append to an existing file

r+ - Read and write to a file 

Using 'r+' means that all writing will take place at the end of the file, and that reading is sequential. The fclose function closes a valid file handle.

Ex:

FILE *p1, *p2; 

p1=fopen(“data”,”r”); 

p2=fopen(“results”,”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. 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.

fclose(file_pointer); 

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.

Ex:

FILE *p1 *p2; 

p1=fopen (“Input”,”w”); 

p2=fopen (“Output”,”r”); 

… 

fclose(p1); 

fclose(p2) 

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.

2. Read from & Write to files

Once a file is open, we can read from it in following ways.

getc and putc functions

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). 

getw & 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 useful when we deal with only integer data. 

fprintf & fscanf functions

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

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

Where fp is 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.  

The general format of fscanf is

fscanf(fp,”controlstring”,list);

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

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: fseek 

fseek function

The general format of fseek function is as follows: 

fseek(file pointer,offset, position);  

This function is used to move the file position to a desired location within the file. File pointer 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 at the position. The position can take the 3 values.                                           

Value

Meaning

0

Beginning of the file

1

Current position

2

End of the file

 3. Deleting Files

The command to delete a file is:

remove (char * szFileName);

This will simply delete the file, with no way to get it back again without an external program.

 

More Examples
/* program to create a file */
#include<stdio.h>
#include<conio.h>
main()
{
FILE *p;
clrscr();
p=fopen("pinki.dat","w");
fclose(p);
}

/* program to store a char into the file */
#include<stdio.h>
#include<conio.h>
main()
{
FILE *p;
char x;
clrscr();
p=fopen("pinki.dat","w");
printf("enter the char ");
x=getchar();
putc(x,p);
fclose(p);
}

/* program to read a char */
#include<stdio.h>
#include<conio.h>
main()
{
FILE *p;
char x;
clrscr();
p=fopen("pinki.dat","r");
x=getc(p);
printf("the char is %c\n",x);
fclose(p);
}

/* program to append a char */
#include<stdio.h>
#include<conio.h>
main()
{
FILE *p;
char x;
clrscr();
p=fopen("pinki.dat","a");
printf("enter the char ");
x=getchar();
putc(x,p);
fclose(p);
}

/* program to read all characters from file */
#include<stdio.h>
#include<conio.h>
main()
{
FILE *p;
char x;
clrscr();
p=fopen("pinki.dat","r");
x=getc(p);
while(!feof(p))
{
printf("%c ",x);
x=getc(p);
}
fclose(p);
}

/* program to store an int into the file */
#include<stdio.h>
#include<conio.h>
main()
{
FILE *p;
int x;
clrscr();
p=fopen("num.dat","w");
printf("enter the number");
scanf("%d",&x);
putw(x,p);
fclose(p);
}

/* program to read an int from the file */
#include<stdio.h>
#include<conio.h>
main()
{
FILE *p;
int x;
clrscr();
p=fopen("num.dat","r");
x=getw(p);
printf("the given number is %d",x);
fclose(p);
}

/* program to append an int value */
#include<stdio.h>
#include<conio.h>
main()
{
FILE *p;
int x;
clrscr();
p=fopen("num.dat","a");
printf("enter the number");
scanf("%d",&x);
putw(x,p);
fclose(p);
}

/* program to read all integers */
#include<stdio.h>
#include<conio.h>
main()
{
FILE *p;
int x;
clrscr();
p=fopen("num.dat","r");
x=getw(p);
while(!feof(p))
{
printf("%d ",x);
x=getw(p);
}
fclose(p);
}

/* program to store different values into file */
#include<stdio.h>
#include<conio.h>
main()
{
FILE *p;
int x;
char y;
float z;
clrscr();
p=fopen("all.dat","w");
printf("enter the int,char,float");
scanf("%d %c %f",&x,&y,&z);
fprintf(p,"%d %c %f",x,y,z);
fclose(p);
}

/* program to read different values from file */
#include<stdio.h>
#include<conio.h>
main()
{
FILE *p;
int x;
char y;
float z;
clrscr();
p=fopen("all.dat","r");
fscanf(p,"%d %c %f",&x,&y,&z);
printf("the values are %d %c %f",x,y,z);
fclose(p);
}

/* program to store a record */
#include<stdio.h>
#include<conio.h>
struct book
{
int bno;
char bname[40];
};
main()
{
struct book b;
FILE *p;
clrscr();
p=fopen("book.dat","w");
printf("enter the bno,bname");
scanf("%d %s",&b.bno,&b.bname);
fwrite(&b,sizeof(b),1,p);
fclose(p);
}

/* program to read a record */
#include<stdio.h>
#include<conio.h>
struct book
{
int bno;
char bname[40];
};
main()
{
struct book b;
FILE *p;
clrscr();
p=fopen("book.dat","r");
fread(&b,sizeof(b),1,p);
printf("%d %s\n",b.bno,b.bname);
fclose(p);
}

/* program to append a record */
#include<stdio.h>
#include<conio.h>
struct book
{
int bno;
char bname[40];
};
main()
{
struct book b;
FILE *p;
clrscr();
p=fopen("book.dat","a");
printf("enter the bno,bname");
scanf("%d %s",&b.bno,&b.bname);
fwrite(&b,sizeof(b),1,p);
fclose(p);
}

/* program to read all records */
#include<stdio.h>
#include<conio.h>
struct book
{
int bno;
char bname[40];
};
main()
{
struct book b;
FILE *p;
clrscr();
p=fopen("book.dat","r");
fread(&b,sizeof(b),1,p);
while(!feof(p))
{
printf("%d %s\n",b.bno,b.bname);
fread(&b,sizeof(b),1,p);
}
fclose(p);
}

/* program to modify a record */
#include<stdio.h>
#include<conio.h>
struct book
{
int bno;
char bname[40];
};
main()
{
struct book b;
int se,i=0;
FILE *p;
clrscr();
p=fopen("book.dat","r+");
printf("enter the bno to be modified");
scanf("%d",&se);
fread(&b,sizeof(b),1,p);
while(!feof(p))
{
i++;
if(b.bno==se)
{
printf("enter the new bno,bname");
scanf("%d %s",&b.bno,&b.bname);
fseek(p,(i-1)*sizeof(b),0);
fwrite(&b,sizeof(b),1,p);
break;
}
fread(&b,sizeof(b),1,p);
}
fclose(p);
p=fopen("book.dat","r");
fread(&b,sizeof(b),1,p);
while(!feof(p))
{
printf("%d %s\n",b.bno,b.bname);
fread(&b,sizeof(b),1,p);
}
printf("the no of records are %d\n",ftell(p)/sizeof(b));
fclose(p);
}

/* program to delete a file */
#include<stdio.h>
#include<conio.h>
struct book
{
int bno;
char bname[40];
};
main()
{
struct book b;
int se;
FILE *p,*q;
clrscr();
p=fopen("book.dat","r");
q=fopen("new.dat","w");
printf("enter the bno to be deleted");
scanf("%d",&se);
fread(&b,sizeof(b),1,p);
while(!feof(p))
{
if(b.bno!=se)
{
fwrite(&b,sizeof(b),1,q);
}
fread(&b,sizeof(b),1,p);
}
fclose(p);
fclose(q);
remove("book.dat");
rename("new.dat","book.dat");
p=fopen("book.dat","r");
fread(&b,sizeof(b),1,p);
while(!feof(p))
{
printf("%d %s\n",b.bno,b.bname);
fread(&b,sizeof(b),1,p);
}
printf("the no of records are %d\n",ftell(p)/sizeof(b));
fclose(p);
}



  « Previous                                                                  Next Chapter »