File - Handling C Programming
File - Handling C Programming
[5].fscanf( ) :
3) Write a c program to create a data file “employee.txt” to name , post and salary 5) Write a c program to read all the data (content) from a data file “Employee.txt” .
of any 5 employees #include<stdio.h>
#include<conio.h>
#include<stdio.h>
void main()
#include<conio.h>
{
void main()
clrscr();
{
FILE *fp;
clrscr();
char name[20],post[20];
FILE *fp;
int salary;
char name[20],post[20];
fp=fopen("employee.txt","r");
int salary,j;
while(fscanf(fp,"%s\t %s\t %d",&name,&post,&salary)!=EOF)
fp=fopen("employee.txt","w");
{
for( j=0;j<5;j=j+1)
{
printf("\n %s\t %s \t%d",name,post,salary);
printf("Enter name,post and salary :");
scanf("%s %s %d",&name,&post,&salary);
}
fprintf(fp,"%s \t %s \t %d \n",name,post,salary);
fclose(fp);
}
getch();
fclose(fp);
}
getch(); 4) Write a c program to create a data file “people.txt” to store name,sex and age of people.
} The program allows to enter data as long as the user wants.
4) Write a c program to read data (content) from a data file “Employee.txt” which #include<stdio.h>
contains name,post,salary of 5 employees. #include<conio.h>
void main()
#include<stdio.h> {
#include<conio.h> clrscr();
void main() FILE *fp;
{ char name[20],sex[20],re;
re = ‘y’;
clrscr();
int age;
FILE *fp; fp=fopen("people.txt","w");
char name[20],post[20]; while( re=’y’)
int salary,j; {
fp=fopen("employee.txt","r"); printf("Enter name,sex and age :");
for(j=1;j<=5;j=j+1) scanf("%s %s %d",&name,&sex,&age);
{ fprintf(fp,"\n %s \t %s \t %d",name,sex,age);
fscanf(fp,"%s\t%s\t %d",&name,&post,&salary) printf(“do you want store more data”);
printf("\n %s\t %s \t%d",name,post,salary); re = getch();
}
}
fclose(fp);
getch(); getch(); }
}