File management
FILE MANAGEMENT IN C
Explain fopen()
· Fopen() function
is use to open a file.
· When we open a
file we must know what we want to do with the file.
·
Example: we must
write a data to the field or read the already existing data.
Syntax:
FILE
*fpt;
Fpt = fopen(“sile_name”,”mode”);
· The variable fpt is a pointer to the data type FILE.
· This pointer contains all of the information about the
file is use as a communication link between the system and program.
· The mode can be r,w or a.
· Both the file name and mode are specifying as a
string.
Example:
FILE
*P1,*P2;
P1=fopen(“data”,”r”);
P1=fopen(“result”,”’w”);
· The first data is
open for reading and the result is open for write.
Explain fclose().
· A file must be
close as soon as all operation on it has been completed.
Syntax:
Fclose(file_pointer);
· Above statement close
the file associated with the file_pointer.
Example:
FILE *p1,*p2;
P1=fopen(“input”,”w”);
P2=fopen(“output”,”r”);
fclose(p1);
fclose(p2);
this program open two file
and close then after all operation on that are completed.
Once a file is close its file
pointer can be reuse for another file.
Explain mode
of file.
There
are three mode of file.
1. r: it is use for
open the file for reading only.
2. w: it is use for
open the file for writing only.
3. a: it is use open
the file for opening or ending data to it.
Explain input output operation on file
Or
Explain:
a.
getc()
b.
putc()
c.
putw() & getw()
d.
fprintf()
e.
fscanf()
getc():
·
getc() is use to
read a character from a file.
·
That has been
open for read mode.
·
Ex: C=getc(fp2)
·
It read a
character from a file whose file pointer is (fp2).
·
The file pointer
move by one character position for every position.
· The getc() will return an end of file marker EOF when
end of the file has been reached.
· Therefore the
reading should be terminated when EOF is in counted.
putc():
· putc() is use to write a character to a file.
· That has been open for write mode.
· putc(c,fp1)
· Now the
character contained in the character variable c to the file which is associated with
FILE pointer FP1.
·
The file pointer
move by one character position for every operation of putc().
Getw() & putw():
· putw() &
getw() is integer oriented function.
· getw() is similar
to the getc() function and is use to read integer value.
· This function is
useful when we deal with only integer data.
· putw() is similar
to the putc() function and are use to write integer value.
Syntex :
putw(integer, fp)
example:
putw(number,f1)
Syntex :
getw(fp)
example:
(number = getw(f1)!=EOF)
·
Read a value
assign the same to number and then test for the end of file mark.
Fpriintf()
& fscanf():
·
Fprintf() &
fscanf() that can handle a group of mixed data simultaneously.
· The function
fprintf() & fscanf() perform I/O operation.
Syantax:
Fprintf(f1,’count string”,list);
Example :
fprintf(f1,”%s
%d %f”,name,age,8.5);
·
The fp is a file
pointer associated with a file that has been opened for writing.
· The control
strings contain output specification for the items in the list.
Syntax:
fscanf(fp,”controlstring”,list);
· fscanf() is use the reading of the item in the list
from the file specify by fp according to the specification contain in the
control string.
Example
fscanf(f2,”%s
%d”,item,&quantity);
· When the end of
file is reach it returns the value EOF.
· Data is read
using the function fscanf() from the file.
Error
handling during I/O operation Or
Which error may be occurring in a file ?
It is possible that an error may occur during input
output operation on a file.
1.
Trying to read the file beyond the end of mark.
2.
Device overflow.
3.
Trying to use a
file that hasn`t been opened.
4.
Trying to perform
operation on a file when the file is opened for another type of operation.
5.
Open a file but with an invalid file name.
6.
Attempting to
write protected file.
· If we fail to check such read and write error a
program may be have abnormally when an error occur.
· The feof() function can be use test for end of file
condition.
· The error function report the status of the file
indicated.
Example of feof():
If
(feof (fp))
printf(“end of data”);
Example of ferror();
if(ferror(fp)!=0)
printf(“an error has occurred”);
Explain Ftell():
· ftell () is used to find the position of the file pointer
in the file with respect to the beginning of the file.
· This function display the current position
of a file.
· n=ftell(fp);
· n would give the current position of pointer.
Explain Rewind():
·
Rewind() takes a
file pointer and resets it to the starting position of the file.
Example
Rewind(fp);
N=ftell(fp);
·
Would assign 0 to N because the file position has been set to the starting position of the file by rewind().
· The first byte of the file is labeled as 0, second as 1, and so on.
·
This function
helps to read a file more than once, without having to close and open the file.
Explain Fseek():
· Fseek() function is used to move the file position to
a described location within the file.
Syntax: fseek(file_ptr,offset,position);
· File_ptr is a pointer to the file concerned, offset is
a number or variable of type long, and position is an inte
· ger number.
· The offset specify the number of position (bytes) to
be move from the location specify by position.
· The position can take one of the value from the following three values.
· The offset may be positive, meaning move forward , or
negative, meaning move backward.
· When the operation is successful fseek returns zero.
· If we attempt to move the file pointer beyond the file
boundaries, as an error occurs and fseek returns -1.
Explain Append():
· Append is use to add on item to the file.
· An execution the program request for file name to
which data is to be appeared.
· Appending the items the position of the last character
in the file in assign to n and then the file is close.