Tuesday, 22 September 2015

Write a C 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.

Write a C 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.
1:  #include<stdio.h>  
2:  #include<conio.h>  
3:  #include<process.h>  
4:  struct stud  
5:  {  
6:        int rno;  
7:        char *nm;  
8:  };  
9:  void main()  
10:  {  
11:        struct stud *s;  
12:        int n,i;  
13:        FILE *fp,*fp1;  
14:        clrscr();  
15:        printf("Enter how many record you want to input : ");  
16:        scanf("%d",&n);  
17:        s=(struct stud *)malloc(n*sizeof(struct stud));  
18:        fp=fopen("STUD.txt","w");  
19:        for(i=0;i<n;i++)  
20:        {  
21:              printf("\n\tInformation for student : %d\n",i+1);  
22:              printf("Enter Roll No : ");  
23:              scanf("%d",&s[i].rno);  
24:              printf("Enter Name : ");  
25:              fflush(stdin);  
26:              gets(s[i].nm);  
27:              fprintf(fp,"%5d %-20s\n",s[i].rno,s[i].nm);  
28:        }  
29:        fclose(fp);  
30:        fp=fopen("STUD.txt","r");  
31:        fp1=fopen("DATA.txt","w");  
32:        printf("\nContent of the STUD.txt file is\n");  
33:        printf("Roll No  Name\n");  
34:        printf("---------------------------\n");  
35:        while(!feof(fp))  
36:        {  
37:              fscanf(fp,"%5d %20s\n",&s[i].rno,s[i].nm);  
38:              fprintf(fp1,"%5d %-20s\n",s[i].rno,s[i].nm);  
39:              printf("%7d  %-20s\n",s[i].rno,s[i].nm);    }  
40:        fcloseall();  
41:        getch();  
42:  }  
43:  Output:  
44:  Enter how many record you want to input : 3  
45:        Information for student : 1  
46:  Enter Roll No : 1  
47:  Enter Name : Kartik  
48:        Information for student : 2  
49:  Enter Roll No : 2  
50:  Enter Name : Umesh  
51:        Information for student : 3  
52:  Enter Roll No : 3  
53:  Enter Name : Mehul  
54:  Content of the STUD.txt file is  
55:  Roll No  Name  
56:  ---------------------------  
57:     1  Kartik  
58:     2  Umesh  
59:     3  Mehul  

No comments:

Post a Comment