Sometimes, we require some specific input data to be generated several numbers of times. The operations we use in a C program are not stored anywhere. But the programs that we write are to store the information collected from the program. We can store the programs in the local file system which is volatile and can be accessed each time.
Why files are needed?
- When a program is closed, the entire data will be lost whereas storing in a file will preserve your data even if the program is closed.
- It will take a lot of time to enter a large number of data.
However, if you have a file having all the data,we can easily get the contents from the file using a few commands. - Data can be easily moved from one computer to another without any changes.
Types of Files
There are two types of files that we can deal with:
- Text files
- Binary files
1. Text files
These are the normal .txt files. They can be created using any simple text editors like Notepad. We can see all the contents within the file as plain text when we open these files. One can easily edit or delete the contents of these files.
It takes minimum effort to maintain them and they are easily readable. They take up more storage space but provide leasst security.
2. Binary files
Binary files are the files having .bin extension. They don’t store data in plain text, they store it in the binary language,in the form of 0’s and 1’s.
Binary files are not easily readable and hence provides better security than text files. They can also hold a higher amount of data.
Operations on Files
- Creating a new file
- Opening an already existing file
- Reading from a file
- Writing in the file
- Deleting the file
There are function is C language to perform these operation on the files.
Functions in File Handling
No. | Function | Description |
---|---|---|
1 | fopen() | opens new or existing file |
2 | fprintf() | write data into the file |
3 | fscanf() | reads data from the file |
4 | fputc() | writes a character into the file |
5 | fgetc() | reads a character from file |
6 | fclose() | closes the file |
7 | fseek() | sets the file pointer to given position |
Example 1: Write to a text file
#include <stdio.h>
#include <stdlib.h>
int main()
{
int no.;
FILE *fptr;
// use appropriate location from your pc to open the file
fptr = fopen("C:\\sample.txt","w");
if(fptr == NULL)
{
printf("Error!");
exit(1);
}
printf("Enter number: ");
scanf("%d",&no.);
fprintf(fptr,"%d",no.);
fclose(fptr); //command to close the file
return 0;
}
In this program, we take an input no,from the user and store it in the file sample.txt
. After compiling and running this program, a text file sample.txt
is created in C drive of your pc/laptop. You can see the integer you entered, when you open the file,.
Example 2: Read from a text file
#include <stdio.h>
#include <stdlib.h>
int main()
{
int no.;
FILE *fptr;
if ((fptr = fopen("C:\\sample.txt","r")) == NULL){
printf("Error! opening file");
// Program exits if the file pointer returns NULL.
exit(1);
}
fscanf(fptr,"%d", &no.); //reads the data from the file
printf("Value of n=%d", no.); //
fclose(fptr); //closes the file
return 0;
}
The program reads the integer present in the sample.txt
file and prints it onto the screen.
fgetchar()
, fputc()
etc. are other functions that can be used in a similar way.