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 :
FILE *fp;
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 :
fclose(fp);
fclose(fptr);
fclose(tfile);
getc() function is used to read a character in a file. It has the following from:
FILE *tfile;
char c;
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:
FILE *tfile;
char c;
putc(c,tfile);
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:
FILE *fptr;
if(feof(fptr)==0)
printf("\n Data available in the file");
else
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:
FILE *nptr;
int m;
m = getw(nptr);
putw() function is used to write an integer data to a file. It has the following from:
FILE *nptr;
int m;
putw(m,nptr);
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.
rewind(fp);
It's a special area where you can find special questions and answers for CSE students or IT professionals. Also, In this section, we try to explain a topic in a very deep way.