Many applications needs to read and write data to disk based storage system. Because all
the data’s of an application are lost when the program is terminated or the computer is turned off. Such information’s are stored in the form of a data file. A file is a place where one can store related data’s permanently. Data files allow us to store information’s permanently and to access and alter the information when required.
C supports a number of functions that are used in file handling.
Naming a file
Opening a file
Reading data from a file
Writing data to a file
Closing a file
There is two type data files: - stream-oriented and system-oriented. Stream-oriented data files are mostly used. When working with a stream-oriented data file, the first step is to establish a buffer area where the data’s will be stored temporarily. The buffer area is established by the statement: FILE *ptr; Here FILE (uppercase is required) is a special structure type defined within stdio.h header file and the pointer ptr points the beginning of the buffer area. A data file must be opened before it can be created or processed. This associates the file name with the buffer area (pointer). It also specifies how the data file will be utilized – read only file, write only file or both. The library function fopen () is used to open a file. This function takes two arguments – the first being the file name as a string and the second argument is the mode as a string in which the file will be opened. The file open mode must be any one of the followings: -
“r” Open an existing file for read only
“w” Open a new file for writing. If a file with the same
exists (the name specified as the first argument) then
it will be destroyed and a new file will be created.
“a” Open an existing file for appending. If the file does not
exist then a new file will be created.
“r+” Open an existing file for both reading and writing
“w+” Open a new file for both reading and writing. If the file
exists then the file will be destroyed and a new file
will be created.
“a+” Open an existing file for reading and appending. If the
file exists then the file will be destroyed and a new file
will be created.
Functions in 'C' for File Management
fopen()
fclose()
getc()
putc()
fscanf()
fprintf()
fgets()
fputs()
fread()
fwrite()
fseek()
ftell()
feof()
rewind()
fopen()
No comments:
Post a Comment