Virajsinh

FileHandling_4_Insert

Nov 17th, 2017
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.01 KB | None | 0 0
  1. //insert Data in Text File
  2.  
  3. #include<conio.h>
  4. #include<stdio.h>
  5.  
  6. typedef struct student
  7. {
  8.     int roll, mark;
  9.     char name;
  10.  
  11. }stud;
  12.  
  13. void main()
  14. {
  15.     stud stud;
  16.     FILE *fp;
  17.     char ch;
  18.     char str[999];
  19.  
  20.     clrscr();
  21.  
  22.     fp=fopen("Student.txt","a");
  23.  
  24.     if(fp==NULL)
  25.     {
  26.         printf("\n File Opening Error");
  27.     }
  28.  
  29.     do
  30.     {
  31.         clrscr();
  32.         printf("\n Enter Student Roll : ");
  33.         scanf("%d",&stud.roll);
  34.  
  35.         printf("\n Enter Student Name : ");
  36.         fflush(stdin);
  37.         scanf("%s",stud.name);
  38.  
  39.         printf("\n Enter Student Mark : ");
  40.         scanf("%d",&stud.mark);
  41.  
  42.         fprintf(fp,"%d\t%s\t%d\n", stud.roll, stud.name, stud.mark);
  43.  
  44.         printf("\n Enter Record [Y or N] : ");
  45.         fflush(stdin);
  46.         scanf("%c",&ch);
  47.  
  48.  
  49.     }while (ch == 'Y' || ch== 'y');
  50.  
  51.     printf("\n Records Inserted in Text File.");
  52.     fclose(fp);
  53.     getch();
  54.  
  55.     fflush(stdin);
  56.     fp=fopen("Student.txt","r");
  57.  
  58.     if(fp==NULL)
  59.     {
  60.         printf("\n Not Open File");
  61.     }
  62.  
  63.     printf("\n");
  64.     while (fgets(str,999,fp) !=NULL)
  65.     {
  66.         printf("%s",str);
  67.     }
  68.     fclose(fp);
  69.     getch();
  70.  
  71. }
Add Comment
Please, Sign In to add comment