Saturday 19 March, 2011

Write a program to read data from keyboard, write it to a file named STUDENT again read the same data from STUDENT file and write it into DATA file. Same data should be displayed on the screen


#include<stdio.h>
#include<conio.h>
#include<process.h>
struct stud
{
int rno;
char *nm;
};
void main()
{
struct stud *s;
int n,i;
FILE *fp,*fp1;
clrscr();
printf("Enter how many record you want to input : ");
scanf("%d",&n);
s=(struct stud *)malloc(n*sizeof(struct stud));
fp=fopen("STUD.txt","w");
for(i=0;i<n;i++)
{
printf("\n\tInformation for student : %d\n",i+1);
printf("Enter Roll No : ");
scanf("%d",&s[i].rno);
printf("Enter Name : ");
fflush(stdin);
gets(s[i].nm);
fprintf(fp,"%5d %-20s\n",s[i].rno,s[i].nm);
}
fclose(fp);
fp=fopen("STUD.txt","r");
fp1=fopen("DATA.txt","w");
printf("\nContent of the STUD.txt file is\n");
printf("Roll No Name\n");
printf("---------------------------\n");
while(!feof(fp))
{
fscanf(fp,"%5d %20s\n",&s[i].rno,s[i].nm);
fprintf(fp1,"%5d %-20s\n",s[i].rno,s[i].nm);
printf("%7d %-20s\n",s[i].rno,s[i].nm);
}
fcloseall();
getch();
}
Output:
Enter how many record you want to input : 3
Information for student : 1
Enter Roll No : 1
Enter Name : happy
Information for student : 2
Enter Roll No : 2
Enter Name : Umesh
Information for student : 3
Enter Roll No : 3
Enter Name : Mehul
Content of the STUD.txt file is
Roll No Name
---------------------------
1 happy
2 Umesh
3 Mehul

No comments:

Post a Comment