Many real life problems involve large volume of data and in such situations the console oriented input output function major problems.
Problems that are associated with console oriented Input output these are
- Time consuming to handle large volume of data through terminals.
- Data is lost when either the program is terminated or the computer is turn off.
To handle these problem data can be stored on the disks and read whenever necessary without destroying the data.So file is need to store that data.A file is a place on the disk where a group of related data is stored.
The operation can be performed on file that are
- creation of new file.
- opening a new file.
- reading from a file.
- writing from a file.
- Deleting the file.
File can be open in the following modes
- r means opens text file in read mode.
- w means opens text file in write mode.
- a means open text file in append mode.
- r+ opens text file in read and write mode.
- w+ opens a text file in read and write mode.
- a+ opens a text file in read and write mode.
- rb open binary file in read mode.
- wb opens a binary file in write mode.
- ab opens a binary file in read and write mode.
- rb+ opens a binary file in read and write mode.
- wb+ opens a binary file in read and write mode.
- ab+ opens a binary file in read and write mode.
Mode of file during creation set according to the need of user.
Need file handling function to access the file in disk that are
fopen() create a new file for use or existing file.
fclose() closes a file which has been opened for use.
getc() reads a character to a file.
putc() Writes a character to a file.
fprintf() Writes a set of data values to a file.
fscanf() reads a set of data values from a file.
getw() Reads a integer from a file.
putw() Writes an integer to a file.
fseek() Sets the position to a desired point in a file.
ftell() Gives the current position in the file.
rewind() Sets the position to the beginning of the file.
Random Access to files
Reading and writing data sequentially in file not always there are occasions when only accessing only a particular part of a file and not in reading or writing other parts.
If user want to access particular part of (or randomly access the file) use file access function for random access that are
- fseek()
- ftell()
- rewind()
fseek function set the position to a desired point in a file.ftell function gives the current position in the file.rewind sets the position to the beginning of the file.Using that function randomly access any part of the file.That is also called random access function of file.
n=ftell(fp)
n would give the offset of the current position.This means that n bytes have already been read.
rewind(fp);
n=ftell(fp);
would assign 0 to n because the file position has been set to the start of the file by rewind.first byte in the file numbered as 0 ,second as 1 and so on.This function helps us in reading a file more than once without having to close the file.
fseek(filepointer,offset,position);
fseek function is is used to move the file position to a desired location within the file.
filepointer is a pointer to the file concerned.
offset is a number or variable type long .
position is an integer number.
position can take one of the following three values
value=0 (Beginning of file).
value=1(Current position).
value=2(End of file).
The offset may be positive meaning move forwards.
The offset may be negative meaning move backwards.
fseek(fp,0L,0) means Go to the beginning.
fseek(fp,0L,1) means Stay at the current position.
fseek(fp,0L,2) means Go to the end of the file.
fseek(fp,m,0) means Move to m+1 bytes in the file.
fseek(fp,m,1) means Go forward by m bytes.
fseek(fp,-m,1) means Go backward by m bytes from the current position.
fseek(fp,-m,2) means Go backward by m bytes from the end.(position the file to the mth character from the end).
For example
A file m.c that is store data "my school" and change the file data "my college" using random function of file fseek(),ftell(),rewind().
According to the code output is
In this code first file store string "my school" that is change with "my college" using random access function fseek(),ftell(),rewind. first set the position of file pointer using fseek from the starting and then apply change. rewind() file function is used to set the file pointer at the beginning.
0 Comments