File Management


A file is a collection of bytes stored on a secondary storage device, which is generally a disk.
The collection of bytes may be
Characters, words, lines, paragraphs.
Fields and records belonging to a database.
Pixels from a graphical image.
In C File is used to handle large volume of data.

We can perform following operations on file:
(1) Create file
(2) Open file
(3) Read data from file
(4) Write data to file
(5) Close file

     

File Management Functions


Various Function of File Management are:

fopen ()

fopen () function is used to create a new file or open an existing file.
The general syntax for opening a file is given below:
FILE *fp;
fp= fopen (“filename”,” mode”);
Here,
FILE
is a structure which is defined inside the I/O Library.
fp is a pointer of type FILE.
Filename is the name of the file which we want to create or open
Mode indicates in which mode you want to open the file. Various Modes are:
r:read
w:write
a:append

fclose ()

fclose () function is used to close the file.
The general syntax for closing the file is given below:
fclose (fp);
Here
fp
is a pointer using which we have opens the file.
It is necessary to close the file to prevent the misuse of the file.

fprintf ()

This function is used to write set of data to the file.
We can write characters, integer, float etc to the file using this function.
Syntax:
fprintf (Filepointer,”control string”, list);
Here,
Filepointer
is the pointer using which we have opens the file.
Control string contains output specification for the items in the list.
The list may includes variable, constants and string

Example:
char name[20];
int age;
FILE *Fp;
Fp=Fopen (“abc.txt”,”w”);
printf (“Enter Name :”);
scanf (“%s”,name);
printf(“Enter Age:”);
scanf(“%d”,&age);
fprintf (fp,”%s%d”,name,age);
fclose (Fp);


fscanf ()

This function is used to read set of data from the file.
We can read characters, integer, float etc from the file using this function.
Syntax:
fscanf (Filepointer, ”control string”, list);
Here,
Filepointer
is the pointer using which we have opens the file.
Control string contains input specification for the items in the list.
The list may includes variable, constants and string

Example:
char name[20];
int age;
FILE *Fp;
Fp=Fopen (“abc.txt”,”r”);
printf (“Enter Name :”);
scanf (“%s”,name);
printf(“Enter Age:”);
scanf(“%d”,&age);
fprintf (fp,”%s%d”,name,age);
fclose (Fp);
Fp=Fopen (“abc.txt”,”r”);
fscanf(fp,”%s%d”,name,&age);
printf(“Name=%s\n”,name);
printf(“Age=%d”,age);
fclose(fp);


putc ()

This function is used to write a single character to the file.
Syntax:
putc (character, Filepointer);
Here,
Character
is the character which we want to write into the file.
Filepointer is the pointer using which we opens the file.

Example:
FILE *Fp;
Fp = fopen (“abc.txt”, ”w”);
putc (‘a’, Fp);
fclose (Fp);


getc ()

This function is used to read a single character from file.
Syntax:
Character = getc (Filepointer);
Here,
Character
is the character which we want to read from the file.
Filepointer is the pointer using which we have opens the file.

Example:
char c;
FILE *Fp;
Fp = fopen (“abc.txt”, ”r”);
c = getc (Fp);
Printf (“Character is %c” , c);
fclose (Fp);


fputc ()

This function is similar to putc () function.
This function is used to write a single character to the file.
Syntax:
fputc (character, Filepointer);
Here,
Character
is the character which we want to write into the file.
Filepointer is the pointer using which we opens the file.

Example:
FILE *Fp;
Fp = fopen (“abc.txt”, ”w”);
fputc (‘a’, Fp);
fclose (Fp);


fgetc ()

This function is similar to getc () function.
This function is used to read a single character from file.
Syntax:
Character = fgetc (Filepointer);
Here,
Character
is the character which we want to read from the file.
Filepointer is the pointer using which we have opens the file.

Example:
char c;
FILE *Fp;
Fp = fopen (“abc.txt”, ”r”);
c = fgetc (Fp);
Printf (“Character is %c” , c);
fclose (Fp);


fseek ()

This function is used to move the cursor (pointer) to the desired position within the file.
If the operation performed using fseek () is successful then it returns zero otherwise it returns negative value.
Syntax:
fseek (Filepointer, offset, Position)
Here,
Filepointer
is a pointer using which we opens the file.
Offset is a number or variable of type long. The offset specifies the number of position to be moved from the location specified by position. The offset may be positive (move forward) or negative (move backward).
Position is an integer number. The position may have following values:
0 means Beginning of file
1 means current position
2 means end of file

ftell ()

This function is used to retrieve the current position of the cursor (pointer) in the file.
It returns a value of type long indicating position of the pointer within file.
Syntax:
int n;
n=ftell (Filepointer);
Here,
Filepointer
is a pointer using which we opens the file.

feof ()

This function is used to determine weather the end of the file has been reached or not.
When a file is being read sequentially one line, or one piece of data at a time this function can be used to check end of the file has come or not.
This function returns nonzero value when the end of file detected and zero value otherwise.
Syntax:
feof (Filepointer);
Here,
Filepointer
is a pointer using which we open the file.

Example:
if (!feof(Fp))
printf(“End of File”);

Download Projects


Download Programs