Virajsinh

FileHandling_6_Update

Nov 20th, 2017
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.27 KB | None | 0 0
  1. //Update Record From Text
  2.  
  3. #include<conio.h>
  4. #include<stdio.h>
  5.  
  6. typedef struct student
  7. {
  8.     int roll, mark;
  9.     char name[80];
  10. }stud;
  11.  
  12. void main()
  13. {
  14.     stud stud;
  15.     FILE *fp1, *fp2;
  16.     int found = 0, find;
  17.  
  18.     clrscr();
  19.  
  20.     fp1=fopen("Student.txt","r");
  21.     if(fp1==NULL)
  22.     {
  23.         printf("\n File Opening Error");
  24.         //exit(1);
  25.     }
  26.  
  27.     fp2=fopen("temp.txt","w");
  28.     if(fp2==NULL)
  29.     {
  30.         printf("\n File Creating Error");
  31.         //exit(1);
  32.     }
  33.  
  34.     printf("\n Enter The Roll : ");
  35.     scanf("%d",&find);
  36.  
  37.     while(1)
  38.     {
  39.         fscanf(fp1,"%d\t%s\t%d\n", &stud.roll, stud.name, &stud.mark);
  40.  
  41.         if(stud.roll==find)
  42.         {
  43.             found = 1;
  44.             printf("\n Details Found");
  45.             printf("\n Roll : %d", stud.roll);
  46.             printf("\n Name : %s", stud.name);
  47.             printf("\n Marks : %d", stud.mark);
  48.  
  49.             printf("\n Enter Name : ");
  50.             scanf("%s", stud.name);
  51.             printf("\n Enter Mark : ");
  52.             scanf("%d",&stud.mark);
  53.  
  54.             fprintf(fp2,"%d\t%s\t%d\n", stud.roll, stud.name, stud.mark);
  55.         }
  56.         else
  57.         {
  58.             fprintf(fp2,"%d\t%s\t%d\n", stud.roll, stud.name, stud.mark);
  59.         }
  60.         if(feof(fp1))
  61.         {
  62.             break;
  63.         }
  64.     }
  65.  
  66.     fclose(fp1);
  67.     fclose(fp2);
  68.     remove("Student.txt");
  69.     rename("temp.txt","Student.txt");
  70.     if(found==0)
  71.     {
  72.         printf("\n Record %d Not Found", find);
  73.     }
  74.     printf("Record Updated");
  75.     getch();
  76. }
Add Comment
Please, Sign In to add comment