We frequently use files for storing our data and reading data.In order to use files in c programming for storing data and reading data from file we have to learn FILE I/O(Input and output).As we know we have many or say lots of files stored in our computer so we have to specify that which file we want to use.when we want to open a file then we have to also specify that in which way we want to use it for Eg : read,write,append etc.We can open file by the help of file Pointer.Now let us see how to define file pointer:
"FILE *file_pointer;" or "FILE *fptr;" or "FILE *fp;" or "FILE *fp1;" OR anything you like.There are also modes to open files.Following are some modes to open files.
"FILE *file_pointer;" or "FILE *fptr;" or "FILE *fp;" or "FILE *fp1;" OR anything you like.There are also modes to open files.Following are some modes to open files.
Mode | Explaination |
r | Opens an text file for reading |
w | Opens an text file for writing |
a | Opens an text file for appending data |
r+ | Opens an text file for reading and also writing |
w+ | Opens an text file for reading and also writing |
a+ | Opens an text file for reading and also writing |
rb | Opens an binary file for reading |
wb | Opens an binary file for writing |
ab | Opens an binary file for appending data to end |
rb+ | Opens an binary file for reading and writing data. |
wb+ | Opens an binary file for reading and writing data. |
ab+ | Opens an binary file for reading and writing data. |
Here fptr,fp,fp1 are file pointers.You can give any name to file pointers.Constants such as FILE, NULL and EOF are stored in <stdio.h> header file.Now let us see Some functions to handle files.
following are some of the functions to handle file.
Function | Use |
fopen() | This function is used to open a file. |
fclose() | this function is used to close any opened file. |
getc() | this function reads a character from file. |
putc() | this function writes character to file. |
fscanf() | this function reads the bunch of data from file. |
fprintf() | this function writes the bunch data to file. |
fseek() | this function is used to set the cursor position at the desired point. |
ftell() | this function gives the current position in the file. |
rewind() | this function sets the cursor position at the beginning of file. |