In C language, using file handling data can be stored permanently in data file on our secondary storage. It allows user to retrieve or access data when ever required. As we have discussed here, we can store or write any numbers of records depending upon the requirements of the user. The user requirements can be storing single records, storing n-records or even storing records until user press ‘Y’.
Here, in these program example we will store name, post and salary of single and multiple employee in data file named “employee.txt”
#include<stdio.h>
int main()
{
char n[10],p[10];
float s;
FILE *fptr;
fptr = fopen("employee.txt","w");
printf("Enter name post and salary");
scanf("%s %s %f",n, p, &s);
fprintf(fptr,"%s %s %f \n",n, p, s);
fclose(fptr);
return 0;
}
The above program will first open data file named “employee.txt” in writing mode. The statement ‘scanf’ stores data which is supplied from the user in main memory temporarily i.e. RAM. Since, main objective of file handling in C is to store data in secondary storage so, ‘fprintf’ statement is used to stores user entered data from RAM to Secondary storage permanently.
As above program stores only one record. So, in order to store multiple records depending upon user condition. We need to add some loop. to Let us assume, if we want to stores 100 records then same program can be changed as following.
#include<stdio.h>
int main()
{
char n[10],p[10];
float s;
int i;
FILE *fptr;
fptr = fopen("employee.txt","w");
printf("Enter name post and salary");
for (i=1;i<=100;i++)
{
scanf("%s %s %f",n, p, &s);
fprintf(fptr,"%s %s %f \n",n,p,s);
}
fclose(fptr);
return 0;
}
In above program, we repeat ‘scanf’ and ‘fprintf’ 100 number of times using for loop. It means at i=1 first record is stored in variable n, p and s temporarily using ’scanf’. At same time the data are stored in data file using ‘fprintf’. This process repeat for 100 times thus, stores 100 records on data file.
This program is similar to storing 100 records, instead of repeating for loop for 100 times now we repeat for n-number of time. Since, the value of ’n’ is unknown we take that value as input from the user before loop.
[Note: Since ’n’ variable is already used for name, here in this program we use ‘x’ to represent total number.]
#include<stdio.h>
int main()
{
char n[10],p[10];
float s;
int i,x;
FILE *fptr;
fptr = fopen("employee.txt","w");
printf("enter any number");
scanf("%d",&x);
printf("Enter name post and salary");
for (i=1;i<=x;i++)
{
scanf("%s %s %f",n,p,&s);
fprintf(fptr,"%s %s %f \n",n,p,s);
}
fclose(fptr);
return 0;
}
While storing ’n’ records we entered value of ’n’ first then, we repeat loop as per the value supplied by the user. But in this case, we will store one record and ask if he/she want to store more data or not. If user presses “Y” or “y” then we will store another set of records. Similarly this program allows user to stores records as per their requirement by simply pressing ‘y’. If user presses key rather than ‘“Y” or “y”’ then, loop will stop repeating and no more records will be added. This can be done as following
#include<stdio.h>
#include<string.h>
int main()
{
char n[10], p[10], ch[10];
float s;
FILE *fptr;
fptr = fopen("record.txt","w");
printf("Enter name post and salary”);
do
{
scanf("%s %s %f", n, p, &s);
fprintf(fptr,"%s %s %f \n",n, p,s);;
printf("Do you want to continue");
scanf("%s",ch);
} while(strcmp(ch,"y") == 0 || strcmp(ch,"Y") == 0);
fclose(fptr);
return 0;
}
In above, we write ‘scanf’ and ‘fprintf’ inside do while loop. These statement store one record supplied by the user. If user want to continue to store more data then he/she presses “Y” or “y”. The choice entered by the user is store in string variable ‘ch’. Then the condition inside the ‘while’ is checked. String handling function ‘strcmp’ is used to compare users choice with “Y” or “y”. While comparing if the ‘strcmp’ function returns 0 then, do while loop continues and another set of records is stored otherwise not.
[Note: To use ’strcmp’ we should always add #include header file]
Since, In above program we treat user choice to continue as string. Hence we should use string handling function but, if we treat user choice as character then we don’t have to use string handling function. As choice is of user is alway single character i.e. “Y” or “y” we can write above program as:
#include<stdio.h>
int main()
{
char n[10], p[10], ch;
float s;
FILE *fptr;
fptr = fopen("record.txt","w");
printf("Enter name post and salary”);
do
{
scanf("%s %s %f", n, p, &s);
fprintf(fptr,"%s %s %f \n",n, p,s);;
printf("Do you want to continue");
scanf(“%c”, &ch);
} while(ch == ‘y’ || ch == ‘Y’);
fclose(fptr);
return 0;
}
Remember:
Strings are array of characters, they are initialized with array size. For eg. char ch[10];
For string we use %s format specifier without ‘&’ while storing data, but if character are initialized without array for e.g. char ch; Since single character is not an array we use %c as format specifier with ‘&’. Characters can be directly compared with out string function.
Click here to for C program Table of Content (Main Menu)