Virajsinh

FileHandling_5_Search

Nov 20th, 2017
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.78 KB | None | 0 0
  1. //Search Record From 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. }stud;
  11.  
  12. void main()
  13. {
  14.     stud stud;
  15.     FILE *fp;
  16.     int found = 0, find;
  17.    
  18.     clrscr();
  19.     fp=fopen("Student.txt","r");
  20.    
  21.     if(fp==NULL)
  22.     {
  23.         printf("\n File Opening Error");
  24.         //exit(1);
  25.     }
  26.    
  27.     printf("\n Enter The Roll : ");
  28.     scanf("%d",&find);
  29.    
  30.     while(1)
  31.     {
  32.         fscanf(fp,"%d\t%s\t%d\n", &stud.roll, stud.name, &stud.mark);
  33.        
  34.         if(stud.roll==find)
  35.         {
  36.             found = 1;
  37.             printf("\n Details Found");
  38.             printf("\n Roll : %d", stud.roll);
  39.             printf("\n Name : %s", stud.name);
  40.             printf("\n Marks : %d", stud.mark);        
  41.         }
  42.         if(feof(fp))
  43.         {
  44.             break;
  45.         }
  46.     }
  47.     if(found==0)
  48.     {
  49.         printf("\n Details Not Found");
  50.     }
  51.    
  52.     fclose(fp);
  53.     getch();
  54. }
Add Comment
Please, Sign In to add comment