C Control StatementsC FunctionsC ArrayC PointerC Dynamic MemoryC StringC MathC StructureC UnionC File HandlingC PreprocessorC Command LineC Misc |
file handling in cSo now till we only use input or output data on console but The input and output operations that we have perform so far were done by keyboard and screen only. now till the data was input through keyboard and output on the screen. After termination of the program all we have enter data is lost because the primary memory is volatile. If data has to use later then it becomes necessary it stored in permanently storage device. So In c there are many I/O library function by which data can be stored on a secondary device or on the disk. The stored data can be access or read whenever required. File handling in c language enables operations to create, read, update and delete the data or files stored on disk or local file system by our c program. There are some operations can be perform on file
functions used for file I/O operations
Open file fopen() you must open the file before perform any operation such as read, write or update so the fopen() used to open a file let us take the example File *fopen(const char *filename,const char *mode); fopen() take two arguments, the first one is the name of the file to be opened and the second is the mode that decides which operation want to perform such as(read,write,append etc) on the file and fopen() returns a pointer of type FILE and if error it returns NULL. The return value of fopen() is assign to the FILE pointer that declare previously let us take example
FILE *fptr1,*fptr2;
“w”(write)
“a”(Apppend)
“r”(read) “w+”(write+read) This mode is same as “w” mode but in this we can also read and modify data of a file. If the file does not exist then the new file is created and file exist already then previous data erased.
“a+”(Append+read)
“r+”(read+write) Closing a File The file that was open using the fopen() function must be closed when not in use or no more operations are perform on it. Declaration: int fclose(FILE *fptr);
We we want to close Multiple files by calling single function Structure of a File program
Take the example which open a file in a write mode
output: file data will be print
fprintf() and fscanf() in c fprintf() and fscanf() click to more fputc() and fgetc() in c fputc() and fgetc() click to more fputs() and fgets() in c |