A file contains data/information which are store permanently in a storage device. Files are classified based on the type of data / information stored in them. The two types of files are -
i) data file (.dat)
ii) text file (.txt)
Data/information stored in a storage device are accessed using a file pointer. The file processing involves the following operations :
A file is declared and the data is accessed using a file pointer. It has the following general form :
Note : Note that the type name FILE is always written in capital letters (upper case) and is defined with a typedef declaration. The file pointer develops a link between the storage device and the program.
fopen() function is used to open a file and set the file pointer to the beginning / end of file ( EOF ). It has following form :
fp = fopen("filename","mode");
Were fp is refers to the name of the file to be opened. filename refers to the name of file. mode refers to the operation mode to access data. The following "mode" are used in data processing :
FILE *fptr, *tfile;
fptr = fopen("Student.dat","w");
tfile = fopen(TextInfo.txt","r");
fclose() function is used to close an active file. It has the following form :
getc() function is used to read a character in a file. It has the following from:
c = getc(tfile);
Note : That the character read from the file is assigned to the char variable c.
putc() function is used to write a character to a file. It has the following from:
Note : That the value of the char variable c is written in the file.
feof() function is used to locate the end of file while reading data. It has the following from:
printf("\n Data available in the file");
printf("\n End of file is reached");
This function will return a 0 when the file contains data and return a non-zero integer when the end of a file is reached.
getw() function is used to read an integer data from a file. It has the following from:
m = getw(nptr);
putw() function is used to write an integer data to a file. It has the following from:
The getchar() function is used to read a character from the keyboard and assign it to a char variable. The character are read from keyboard until the Enter key pressed. This is repeated using a while loop.
fscanf() function is used to read data from a file. It is similar to the scanf() function except that fscanf() is used to read data from the disk.
fscanf(fp,"%d %s %d", &rno, sname, &total);
fprintf() function is used to write data to a file. It is similar to the printf() function except that fprintf() is used to write data to the disk.
fprintf(fp,"%d %s %d", rno, sname, total);
rewind() function is used to move the file pointer to the beginning of a file.
Currently we improved our website User Interface and other contents. Our next update is adding video tutorials on this site so, We create a new YouTube channel, our YouTube channel name is CodeX.
We need some time for make video tutorials (CodeX) so, stay with us and keep supporting and you know already we make a Collaboration with HackIsOn YouTube channel long time ago So, please Subscribe Our Channels