#include<conio.h>
#include<stdio.h>
void main()
{
FILE *fp1;
typedef struct student
{
char name[20];
int age;
}stu;
stu s1;
int i,n;
clrscr();
fp1=fopen("stu.txt","w");
if(fp1==NULL)
{
printf("file creation error\n");
getch();
exit(1);
}
printf("enter the no of students\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter the name and age\n");
scanf("%s%d",s1.name,&s1.age);
fprintf(fp1,"%s %d\n",s1.name,s1.age);
}
fclose(fp1);
fp1=fopen("stu.txt","r");
if(fp1==NULL)
{
printf("file does not exist\n");
getch();
exit(1);
}
else
{
while((fscanf(fp1,"%s%d",s1.name,&s1.age))!=EOF)
{
printf("name=%s age=%d\n",s1.name,s1.age);
}
}
fclose(fp1);
getch();
}
#include<stdio.h>
void main()
{
FILE *fp1;
typedef struct student
{
char name[20];
int age;
}stu;
stu s1;
int i,n;
clrscr();
fp1=fopen("stu.txt","w");
if(fp1==NULL)
{
printf("file creation error\n");
getch();
exit(1);
}
printf("enter the no of students\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter the name and age\n");
scanf("%s%d",s1.name,&s1.age);
fprintf(fp1,"%s %d\n",s1.name,s1.age);
}
fclose(fp1);
fp1=fopen("stu.txt","r");
if(fp1==NULL)
{
printf("file does not exist\n");
getch();
exit(1);
}
else
{
while((fscanf(fp1,"%s%d",s1.name,&s1.age))!=EOF)
{
printf("name=%s age=%d\n",s1.name,s1.age);
}
}
fclose(fp1);
getch();
}
No comments:
Post a Comment