Chapter 4- Programming in C----> File Handling in C
“w” to write/store data in a data file.
“r” to display/read/retrieve/access data from a datafile.
“a” to add/append data in existing datafile.
fptr = fopen (“filename” , ”mode”)
Store/write data
fprintf(fptr , ”format specifiers” ,variables);
1) Create a datafile “student.txt” and store roll number, name, class, address of a patient.
#include <stdio.h>
int main()
{
char n[10], a[10];
int r, c;
FILE *fptr;
fptr = fopen(“student.txt”,”w”);
printf("Enter roll number, name, class, address ");
scanf(“%d %s %d %s”, &r,n, &c,a);
fprintf(fptr, “%d %s %d %s”, r,n, c, a);
fclose(fptr);
return 0;
}
2) Create a datafile “student.txt” and store name, class and marks obtained in 3 different subject for few students/n-students.
#include <stdio.h>
int main()
{
char n[10];
int c, e, ne, m, i, num;
FILE *fptr;
fptr = fopen("student.txt","w");
printf("How many record?");
scanf("%d",&num);
for(i=1;i<=num;i++)
{
printf("Enter name class and 3 marks");
scanf("%s %d %d %d %d",n, &c, &e, &ne, &m);
fprintf(fptr,"%s %d %d %d %d \n",n, c, e, ne, m);
}
fclose(fptr);
return 0;
}
3) Create a datafile “student.txt” and store name, class and marks obtained in 3 different subject until user press “y” / as per user requirement.
#include <stdio.h>
int main()
{
char n[10],ch[3];
int c, e, ne, m;
FILE *fptr;
fptr = fopen("student.txt","w");
do
{
printf("Enter name class and 3 marks");
scanf("%s %d %d %d %d",n, &c, &e, &ne, &m);
fprintf(fptr,"%s %d %d %d %d\n",n, c, e, ne, m);
printf("Press Y to continue");
scanf("%s",ch);
} while (strcmp(ch,"Y") == 0 || strcmp(ch,"y")==0);
fclose(fptr);
return 0;
}
Add/Append data
1) A datafile “student.txt” contain name, class and marks obtained in 3 different subject of few students. Write a C program to add 200 more records.
#include <stdio.h>
int main()
{
char n[10];
int c, e, ne, m, i;
FILE *fptr;
fptr = fopen("student.txt","a");
for(i=1;i<=200;i++)
{
printf("Enter name class and 3 marks");
scanf("%s %d %d %d %d", n, &c, &e, &ne, &m);
fprintf(fptr,"%s %d %d %d %d \n",n, c, e, ne, m);
}
fclose(fptr);
return 0;
}
2) A datafile “student.txt” contain name, class and marks obtained in 3 different subject of few students. Write a C program to add more records until user press “y” / as per user requirement.
#include <stdio.h>
int main()
{
char n[10], ch[3];
int c, e, ne, m;
FILE *fptr;
fptr = fopen("student.txt”,”a”);
do
{
printf("Enter name class and 3 marks");
scanf("%s %d %d %d %d", n, &c, &e, &ne, &m);
fprintf(fptr,"%s %d %d %d %d\n", n, c, e, ne, m);
printf("Press Y to continue");
scanf("%s",ch);
} while (strcmp(ch,"Y") == 0 || strcmp(ch,"y")==0);
fclose(fptr);
return 0;
}
Read/Display/retrieve/access data from a datafile
Syntax:
fscanf(fptr , ”format specifiers” ,variables);
EOF: End of file
1) A datafile “student.txt” contain name, class and marks obtained in 3 different subject of few students. Write a C program to read and display all records.
#include <stdio.h>
int main()
{
char n[10];
int c, e, ne, m;
FILE *fptr;
fptr = fopen("student.txt","r");
printf("Name\tPercentage\n");
while(fscanf(fptr,"%s %d %d %d %d",n,&c,&e,&ne,&m) != EOF)
{
printf("%s %d %d %d %d", n, c, e, ne, m);
}
fclose(fptr);
return 0;
}
2) A datafile “student.txt” contain name, class and marks obtained in 3 different subject of few students. Write a C program to read and display only records whose name is Sita.
#include <stdio.h>
int main()
{
char n[10];
int c, e, ne, m;
FILE *fptr;
fptr = fopen("student.txt","r");
while(fscanf(fptr,"%s %d %d %d %d",n,&c,&e,&ne,&m) != EOF)
{
strlwr(n);
if (strcmp(n,”sita”) == 0)
{
printf("%s %d %d %d %d", n, c, e, ne, m);
}
}
fclose(fptr);
return 0;
}
3) A datafile “student.txt” contain name, class and marks obtained in 3 different subject of few students. Write a C program to read and display only records who pass in all subjects.
#include <stdio.h>
int main()
{
char n[10];
int c, e, ne, m;
FILE *fptr;
fptr = fopen("student.txt","r");
while(fscanf(fptr,"%s %d %d %d %d",n,&c,&e,&ne,&m) != EOF)
{
if (e>=40 && ne>=40 && m>=40)
{
printf("%s %d %d %d %d", n, c, e, ne, m);
}
}
fclose(fptr);
return 0;
}
4) A datafile “student.txt” contain name, class and marks obtained in 3 different subject of few students. Write a C program to read and display only records who fail in any one subject.
#include <stdio.h>
int main()
{
char n[10];
int c, e, ne, m;
FILE *fptr;
fptr = fopen("student.txt","r");
while(fscanf(fptr, "%s %d %d %d %d", n, &c, &e, &ne, &m) != EOF)
{
if (e<40 || ne<40 || m<40)
{
printf("%s %d %d %d %d", n, c, e, ne, m);
}
}
fclose(fptr);
return 0;
}
5) A datafile “student.txt” contain name, class and marks obtained in 3 different subject of few students. Write a C program to read and display only records of all students who secure distinction.
#include <stdio.h>
int main()
{
char n[10];
int c, e, ne, m;
float p;
FILE *fptr;
fptr = fopen("student.txt","r");
while(fscanf(fptr, "%s %d %d %d %d", n, &c, &e, &ne, &m) != EOF)
{
p = (e+ne+m)/3;
if (p>=80)
{
printf("%s %d %d %d %d", n, c, e, ne, m);
}
}
fclose(fptr);
return 0;
}