Explain about Various File Handling Functions in C?

https://www.computersprofessor.com/2016/12/explain-about-various-file-handling.html
|
||||||||||||
In C files are handled with the
library functions. The following are few functions that are used with text
files as well as binary files.
1. fopen ( )
2. fclose ( )
3. fclose all ( )
4. feof ( )
5. ferror ( )
|
||||||||||||
1. Defining and opening a
files:
If we want to store data in a file
in the secondary memory, we must specify certain things about the file, to
the opening system. They include :
1. File name
2. Data structure
3. Purpose
File name is a string of characters that
make up a valid filename. It may contain two parts, a primary name and as
optical period with the extension.
Ex: Input.data, student.C
Data structure of a file is defined
as FILE in the library of standard I/O function definitions. Therefore all
files should be declared as type FILE before they are used.
When we open a file, we must specify
what we want to do with the file.
For ex: we may write data to the
file or read the already existing data.
General format for declaring and
opening a file.
File fp:
fp=fopen ( “file name”, “mode”);
The first statement declares the
variable fp as a pointer to the data type FILE.
The second statement opens the file
named filename and assigns an identifier to the FILE type pointer fp. This
pointer, which contains all the info about the file.
The second statement also specifies
the purpose of opening this file. The mode does this job. mode can be one of
the following.
Note that both the filename and mode
are specified as strings. They should be enclosed in double quotation marks:
Ex: FILE p1, p2;
p1 = fopen ( “data”, “r”);
p2= fopen ( “results”, “w”);
|
||||||||||||
When the mode is writing, a file
with the specified name is created if the file does not exist. The contents
are deleted, if the file already exists.
|
||||||||||||
When the purpose is “appending ”the
file is opened with the current contents safe a file with the specified name
is created if the file does not exist.
|
||||||||||||
If the purpose is reading and if it
exists, then the file is opened with the current contents safe otherwise an
error occurs.
|
||||||||||||
2.Closing a file:
A file must be closed as soon as all
operations on it have been completed. This ensures that all outstanding information
associated with the file is flushed out from the buffers and all links to the
file are broken.
It also prevents any accidental
misuse of the file.
Syntax: fclose ( file pointer);
Ex: fclose ( p1);
Another instance where we have to
close a file is when we went to reopen the same file in a different mode.
|
||||||||||||
3.fcloseall ( ):
The set of all opened files can be
closed with the single function called fcloseall ( ).
Syntax : fcloseall ( );
Error handling during I/O
operations:
It is possible that an error may
occur during I/O operations on a file. The errors many include.
*Trying to read beyond the end-of-file
mark.
*Trying to sue a file that has not
been opened.
*Trying to perform an operation on a
file, when the file is opened for another type of operation.
*Opening a file with an invalid file
name.
We have 2 library functions feof and
ferror that can help us detect I/O errors in the files.
4. feof ( ):
The feof function Can be sued to
test for an end of file condition. It takes a FILE pointer as its only argument
and returns a non zero integer value if all of the data from the specified
file has been read, and returns zero otherwise.
Ex: if ( feof ( fp))
printf ( “end of file”);
5. ferror ( ):
The ferror function Reports the
status of the file indicated. It also takes a FILE pointer as its argument
And returns a nonzero integer if an error has been detected up to that point,
during processing. It returns zero otherwise.
Ex:
if ( ferror ( fp) ! = 0)
printf ( “An error has occurred”);
* The corresponding modes of binary
files are rb, wb, ab, rb+, wb+, ab+.
Whenever a file is opened using open
fn: a file pointer is returned. If the file cannot be opened for some reason,
then the function returns a NULL pointer.
Ex: if ( fp == NULL)
printf ( “file could not be
opened”);
|