Virajsinh

FileHandling_7_Delete

Nov 29th, 2017
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.07 KB | None | 0 0
  1. //Delete 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("stud1.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.         else
  50.         {
  51.             fprintf(fp2,"%d\t%s\t%d\n", stud.roll, stud.name, stud.mark);
  52.         }
  53.         if(feof(fp1))
  54.         {
  55.             break;
  56.         }
  57.     }
  58.  
  59.     fclose(fp1);
  60.     fclose(fp2);
  61.     remove("stud1.txt");
  62.     rename("temp.txt","stud1.txt");
  63.     if(found==0)
  64.     {
  65.         printf("\n Record %d Not Found", find);
  66.     }
  67.     printf("Record Updated");
  68.     getch();
  69. }
Add Comment
Please, Sign In to add comment