Files_new
Files_new
In all C programs considered so far, we assumed that the input data was read from
standard input and the output was displayed on the standard output.
In real life while dealing with large amount of data, it is necessary to store data
permanently, so that user can access and alter the data if required.
File is a place on a disk where data can be stored permanently.
File is a collection of information stored in secondary memory identified by filename.
A program takes the input, processes the input and displays the results or output on
the screen.
File input allows us to take input from already stored data from disk files.
Similarly, file output allows us to store the output in disk files.
File Operations
There are different operations that can be carried on files like
o Creating new file
o Opening an existing file
o Reading from a file
o Writing to a file
o Moving to a specific location in a file
o Closing a file.
File Opening Modes
"r"
Searches file. If the file is opened successfully fopen( ) loads it into memory and sets up a pointer
which points to the first character in it. If the file cannot be opened fopen( ) returns NULL.
"w"
Searches file. If the file exists, its contents are overwritten. If the file doesn’t exist, a new file is
created. Returns NULL, if unable to open file.
"a"
Searches file. If the file is opened successfully fopen( ) loads it into memory and sets up a pointer
that points to the last character in it. If the file doesn’t exist, a new file is created. Returns NULL, if
unable to open file.
"r+"
Searches file. If is opened successfully fopen( ) loads it into memory and sets up a pointer which
points to the first character in it. Returns NULL, if unable to open the file.
Operations possible - reading existing contents, writing new contents, modifying existing contents of
the file.
"w+"
Searches file. If the file exists, its contents are overwritten. If the file doesn’t exist a new file is
created. Returns NULL, if unable to open file.
Operations possible - writing new contents, reading them back and modifying existing contents of
the file.
"a+"
Searches file. If the file is opened successfully fopen( ) loads it into memory and sets up a pointer
which points to the first character in it. If the file doesn’t exist, a new file is created. Returns NULL, if
unable to open file.
Operations possible - reading existing contents, appending new contents to end of file. Cannot
modify existing contents.
---------------------------------------------------------------------------------------------------------------------------------
To open the file call the function fopen( ). It would open a file “PR1.C” in ‘read’ mode, which tells the
C compiler that we would be reading the contents of the file.
To be able to successfully read from a file information like mode of opening, size of file, place in the
file from where the next read operation would be performed, etc. has to be maintained. Since all
this information is inter-related, all of it is gathered together by fopen( ) in a structure called FILE.
fopen( ) returns the address of this structure, which is collected in the structure pointer called fp.
FILE *fp ;
The FILE structure has been defined in the header file “stdio.h” (standing for standard input/output
header file). Therefore, it is necessary to #include this file.
--------------------------------------------------------------------------------------------------------------------------------------
fread() fread() reads into an array , n number of data items of specified size from the stream pointed
by the file pointer.
Syntax : fread(ptr, size, nitems, fp);
Where ptr is the pointer to array into which the data is read.
size is the size of each data item to read
nitem is the number of items to read.
fp is the file pointer from where the data is read.
Eg.fread(ptr,sizeof(struct emp),5,fp)
This will read 5 emp records of respective size from file pointed by fp into array pointed by
ptr.
fwrite() fwrite()writes an array , n number of data items of specified size into the stream pointed by
the file pointer.
Syntax : fwrite(ptr, size, nitems, fp);
Where ptr is the pointer to array which contains the data to write.
size is the size of each data item
nitems is the number of items to write.
fp is the file pointer pointing the file where data is to be written.
Eg.fwrite(ptr,sizeof(struct emp),5,fp)
This will write 5 emp records of respective size to file pointed by fp .
fseek()
Moves file pointer to the byte number offset from the given position in the file
pointed to by fp.
Syntax : int fseek(FILE *fp, int offset, int position);
Where
offset: New position is at the specified number of offset bytes from the beginning,
current position or end of file depending on value of position.
This third argument can be SEEK_CUR, SEEK_END or SEEK_SET.
o SEEK_CUR means move pointer from current position
o SEEK_END means move pointer from end.
o SEEK_SET means move pointer from beginning.
ftell()
This function returns the current position of the file pointer in the file pointed to by
fp.
Syntax : position=ftell(fp);
rewind()
This function repositions the file pointer to the beginning of file.
Syntax : rewind(fp);
-------------------------------------------------------------------------------------------------
Please refer to ‘Let Us C’ book also for this topic as well as other topics.