In C language, as we have discussed using file handling data can be stored permanently in data file on our secondary storage. It also allows user to retrieve or access stored data when ever required. The data from a data file can be accessed according to the user condition i.e whether it can displaying all records or to filtering data from data file we can use file handling in reading mode ’r’
Here, in these program example we will assume a data file named “employee.txt” which contains name, post and salary of few employee. Following program examples helps about how to read and display record from a data file.
#include<stdio.h>
int main()
{
char n[10],p[10];
float s;
FILE *fptr;
fptr = fopen("employee.txt”,”r”);
fscanf(fptr,"%s %s %f \n",n, p, &s);
printf("%s %s %f",n, p, s);
fclose(fptr);
return 0;
}
The above program will first open data file named “employee.txt” in reading mode. The statement ‘fscanf’ access data which was stored in “employee.txt” and loads in the variable n, p and s. This statement only retrieve data from the data file i.e. read data from data file. In order to display the above retrieved data ‘fscanf’ is followed by ‘printf’. This ‘printf’ statement displays the record in scree.
As above program read and display only one record. So, in order to read and display multiple records depending upon user condition. We need to add ‘EOF’ in loop.
Since, we do not know the total number of records in the data file. We do not know how many time we have to access the records to display all. So, to solve this problem we use ‘EOF’ statement which denotes “End of File”. Now we can read and display records until EOF. Hence, all records and be display.
#include<stdio.h>
int main()
{
char n[10],p[10];
float s;
FILE *fptr;
fptr = fopen("employee.txt”,”r”);
while (fscanf(fptr,"%s %s %f \n",n, p, &s) != EOF)
{
printf("%s %s %f",n, p, s);
}
fclose(fptr);
return 0;
}
In above program, ‘fscanf’ retrieves or read records and ‘printf’ display it on screen. This process is repeated unless and until all reading process is finished i.e. when program encounter ‘EOF’ the compiler understands that there are no more datas to read and hence, loop exits and all records are displayed.
The above program read and displays all records. Let use assume that if we want to display only those records whose salary is greater than 5000 then we have to add “if statement” before displaying inside while. The program can be written as
#include<stdio.h>
int main()
{
char n[10],p[10];
float s;
FILE *fptr;
fptr = fopen("employee.txt”,”r”);
while (fscanf(fptr,"%s %s %f \n",n, p, &s) != EOF)
{
if (s>50000)
{
printf("%s %s %f",n, p, s);
}
}
fclose(fptr);
return 0;
}
In above program, ‘fscanf’ retrieves or read records and ‘printf’ display it on screen but here, we have written ‘printf’ inside the “if statement” which means ‘printf’ in this program will execute if and only the condition inside the “if statement” is true. Since salary of an employee is store in variable “s” during “fscanf”, we use if(s>50000) to filter records.
To solve the given program, we know that all the names in the data file are store in variable “n” as the array of character i.e. every thing store in the variable “n” is store as following.
Suppose, we consider a name “David”. It is stored in character array as
n[0] = ‘D’
n[1] = ‘a’
n[2] = ‘v’
n[3] = ‘I’
n[4] = ‘d’
n[5] = ‘\0’
From this we can understand that every name first letter is store in n[0], So before displaying records we can we n[0] to compare first letter with given letter ’S’.
#include<stdio.h>
#include<string.h>
int main()
{
char n[10],p[10];
float s;
FILE *fptr;
fptr = fopen("employee.txt”,”r”);
while (fscanf(fptr,"%s %s %f \n",n, p, &s) != EOF)
{
if (strcmp(toupper(n[0]), ’S’) == 0 )
{
printf("%s %s %f",n, p, s);
}
}
fclose(fptr);
return 0;
}
Here, in this program, since first letter in the records which is on n[0] may be small or upper case so, we convert it into upper case by using “toupper( )” with header file #include, Since, C99 version does not support strupr and strlwr we use toupper and tolower. Similarly, first letter is compared to letter “S” using “strcmp” function with header file #include this compare whether name first letter of record is equal to letter “S” of not. If they are same the function “strcmp” will return 0 that means only those records whose name starts with “S” are only displayed.