File :

Files :

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)

File Processing :

Data/information stored in a storage device are accessed using a file pointer. The file processing involves the following operations :

  • Creation of a file of a specific type
  • Reading / processing a file
  • Append / add information to a file
  • Modify / edit data in a file
  • Delete items in a file
  • Update the file

File Declaration :

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 :

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 :

  • "w" to open a file to write data / information.
  • "r" to open a file to read data / information from the beginning of a file.
  • "a" to open a file to add data / information at the end of an existing file.
  • "w+" to open a file for writing. Data can be read after writing using the same mode.
  • "r+" to open a file for reading and writing data.
  • "a+" to open a file to write information at the end of a file. Data can be read after writing using the same mode.

Syntax :

FILE *fptr, *tfile;
fptr = fopen("Student.dat","w");
tfile = fopen(TextInfo.txt","r");


fclose() Function :

fclose() function is used to close an active file. It has the following form :

fclose(fp);

Syntax :

fclose(fptr);
fclose(tfile);



Handling Characters :

getc() Function :

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 :

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 :

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.


Handling Integers :

getw() Function :

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 :

putw() function is used to write an integer data to a file. It has the following from:

FILE *nptr;
int m;
putw(m,nptr);



getchar() :

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.

Example :

  1. /* Description: text file creation */
  2. #include<stdio.h>
  3. main()
  4. {
  5. char ch;
  6. FILE *fp;
  7. fp=fopen("Sample.txt","w");
  8. printf("\n Type the text press enter key at end. \n\n ");
  9. while((ch=getchar()) !='\n')
  10. putc(ch,fp);
  11. fclose(fp);
  12. puts("\n Data store completed.");
  13. getch();
  14. }

Output :

c language - text file creation

fscanf() Function :

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 :

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 :

rewind() function is used to move the file pointer to the beginning of a file.

rewind(fp);

Computer Science Engineering

Special Notes

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.

CSE Notes