written 5.6 years ago by | • modified 5.6 years ago |
There are various types of files like:text file stored with the extension “txt”,binary files stored with the extension “bin” etc. For file handling or accessing the contents of file,there are certain predefined functions available in the c programming language,
A file represents a sequence of bytes on the disk where a group of Related data is stored. File is created for permanent storage of data. C programming Language can handle files as stream-oriented data(text) files and system oriented Data(binary) files.
An important thing required to access files is the “FILE pointer”. This pointer is used to point to the values stored in the file. A file pointer is hence to be created for accessing the files. The syntax for creating a file pointer is a given below:
FILE *<\identifier for pointer>;
For e.g. FILE *fp;
Hence in every program we write in this section to access files,we will use this kind of pointer declaration. This pointer is used to point the data to be accessed in the file i.e. whenever a data is read or written in the file, it is from the location pointed by the file pointer “fp”.
Function and there use:
1.fopen():This function is used to open a file to be accessed in the program. The file to be opened is to be passed as a string parameter to the function and also the mode of opening the file is to be passed as the string to the function.Hence the syntax of the function with parameters is as given below:
<file pointer identifier>=fopen<”file name>”,<mode of opening the file>”)
For e.g. fp=fopen(“test.txt”,”w”);
The various modes in which file can be opened are as follows
1.“r” indicates that the file is to be opened indicates in the read mode.
2.“w” indicates that the file is to be opened indicates in the write mode.When a file is opened in write mode the data written in the file overwrites the previously stored data in the file.
3.”a” indicates that the file is to be opened in the append mode.In this mode the data written into the file is appended towards the end of the already stored data in the file.The earlier stored data remains as it is.
4.“w+” indicates that the file is to be opened in write and read mode.
5.“r+” indicates that the file is to be opened in read and write mode.
2.fclose():The function is used to close the file opened using the file pointer passed to the function. The syntax with parameters to call this function is as given below:
fclose(<file pointer identifier>);
For e.g. fclose(fp);
This statement closes the file opened using the file pointer variable “fp”. It closes the file opened using the function fopen().
3.feof(): This function returns true or false based on whether the pointer pointing to the file has reached the end of the file or not. The pointer used to point the file has to be passed as a parameter to the function feof(). Also the file has to be opened pointed using the pointer “fp”,before using this function. The syntax of the function with the parameters is as shown below:
feof(<file pointer identifier>);
4.fputc(): This function is used to put a character type data into the opened file using the open() function,pointed by a file pointer. The character to be put into the file as well as the pointer are to be passed as the parameters to this function. The syntax to call this function along with the parameters to be passed is as shown below:
fputc<char type data>,<file pointer identifier>);
For e.g.: fputc(c,fp);
This example will store the character value of the char type data variable.”c” into the opened file and pointed by the pointer fp at the position pointed by the pointer fp in the file.
5.getc(): This function is used to get a character from the file pointed by the corresponding file pointer passed to the function. It is exactly opposite the fputc() function. This function brings the character from the file opened and pointed by the file pointer variable passed to the function. The syntax of the function call with the parameters to be passed is as given below:
getc(<file pointer identifier>);
For e.g. getc(fp);
This function brings the character type data from the opened file using the pointer fp; from the location pointed by the pointer “fp” in that file.
6.rewind() : This function is used to rewind or bring the file pointer variable to the point to the beginning of the file from wherever it is currently pointing in the file. The syntax of the function call with the parameters to be passed is as given below:
rewind(<file pointer identifier>);
For e.g.rewind(fp);
This function rewinds or brings back the pointer “fp” to the beginning of the file from wherever it was pointing in the file opened using the pointer “fp”.
7.Fprintf(): This function is used to store the different data types in the file as the fputc() function is used to store the character in the file. This can be used to store integers ,float,string etc.types of data into the file opened. The function is similar to printf(),except that it writes to the file instead of the monitor.The syntax shown below explains the concept:
fprintf(<file pointer identifier>,”<format specifiers>”,<variable names>);
For e.g.: fprintf(fp,”%d”,x);
This function will print or store the value of integer variable“x” in the file opened using the pointer “fp”. The data will be stored at the location pointed by the pointer variable “fp”.
8.Fscanf(): The function is used to read the different types of the data as the getc()Function is used to read a character from the file. This function can be used to read an integer,float string etc.types of data into the file opened. The function is similar to scanf(),except that it reads from the file instead of the keyboard. The syntax shown below explains the concept:
fscanf(<file pointer variable>,”<format specifiers>”,<address of the variables in which the data is to be read>);
for e.g.: fscanf(fp,”%d”,&x);