Working With Files

Working with Files

Many applications require that information be written to or read from an auxiliary storage device. Such information is stored in the disk (secondary storage) in the form of a data file. Thus, data files allow storing information permanently and accessing and retrieving that information whenever necessary.

Concept of Data File

While a program is processing, the content of the variable is stored in RAM. But, the problem is if such values of the variable are needed in the future, this causes a problem as the value of the variable is only within the program that is being processed. After a program is over, the content of the RAM gets erased. To overcome this problem, we can use a data file to save the content into it to retrieve content from the data file whenever we want it.

File Operation Mode

There are different ways in which a file is to be opened:

File opening mode Description
r Used to open the file for reading provided the file must exist to read it.
w Used for opening the file for writing. If the file exists, its contents will be erased and if the file doesn’t exist, then it will be created.
a Used for opening the file to append (to add content from the last offset) if the file exists but if the file doesn’t exist,a new file will be created.
r+ Used for opening the file for both reading and writing. The file must already exist before.

Sequential file

In this technique, the content is stored and read back sequentially in a usual way using a loop and some functions using rewind(), ftell(), etc.

Random file

In this technique, the content is stored sequentially and read back the content from any position of the file. It can be performed by using some functions like fseek(), ftell(), etc.

File Manipulation Function

C programming offers various operations associated with file handling. They are:

  • Creating a new file: fopen()
  • Opening an existing file in your system: fopen()
  • Closing a file: fclose()
  • Reading characters from a line: getc()
  • Writing characters in a file: putc()
  • Reading a set of data from a file: fscanf()
  • Writing a set of data in a file: fprintf()
  • Reading an integral value from a file: getw()
  • Writing an integral value in a file: putw()
  • Setting a desired position in the file: fseek()
  • Getting the current position in the file: ftell()
  • Setting the position at the beginning point: rewind()

C program to demonstrate the use of reading and writing by using function getw() and putw().

#include<stdio.h>  
#include<conio.h>  
int main()  
{  
int ch;  
 FILE *fp;  
 fp= fopen("file1.txt","w");  
 putw(65,fp);  
 fclose(fp);  
 fp= fopen("file1.txt","r");  
 ch= getw(fp);  
 printf("%d", ch);  
 fclose(fp);  
}

Appending on the data file

Appending on the data file means adding more contents at the end of the existing file, i.e., wherever the EOF flag is placed from that point, contents are added.
The file opening mode must be “a” which stands for append. However, “a+” mode can also be used for appending as well as for reading the file.
For example: fptr=fopen(“c:\\name.txt”,“a”);
where c:\\name.txt is file which is going to be created if it hasn’t been created before and if it has been created the offset pointer will move at EOF.

#include<stdio.h>  
#include<conio.h>  
void main()  
{  
 FILE *fp;  
char *str;  
 fp= fopen("c:\\name.txt","a");  
 printf("Enter name of the student:");  
 scanf("%[^\n]", str);  
 fprintf(fp,"%s\n",str);  
 fclose(fp);  
}  

Output:
Enter the name of the student: Pallavi