469,086 Members | 1,146 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,086 developers. It's quick & easy.

C text file for storing and searching file

This is my code for the program. however the search section does not seem to be working, no matter even if the name is present in the file it still shows not found. is my syntax for while wrong? ive been trying to debug this for the whole day and i still cant fix it. :c please help
the problem is in searchrecord () function starting from line 130
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<string.h>
  4. #include<stdlib.h>
  5. #include<ctype.h>
  6. #define N 100
  7. void addrecord();
  8. void deleterecord(void);
  9. void searchrecord(void);
  10. void modifyrecord(void);
  11. void viewrecord(void);
  12. void mainmenu(void);
  13.  
  14. struct person{
  15. char name[N],address[N],email[N];
  16. long int citino,phone;
  17. struct dob{
  18. int dd,mm,yy;
  19. }d;
  20. };
  21. char cho;
  22. FILE *ptr;
  23.  
  24. int main()
  25. {
  26.     void welcomeMessage();
  27.     mainmenu();
  28.     return 0;
  29. }
  30. void welcomeMessage()
  31. {
  32.     system("COLOR 1F");
  33.     printf("\n\n\n\n\n");
  34.     printf("\n\t\t\t        =============================================");
  35.     printf("\n\t\t\t        |                  WELCOME                  |");
  36.     printf("\n\t\t\t        |                    TO                     |");
  37.     printf("\n\t\t\t        |                  RECORD                   |");
  38.     printf("\n\t\t\t        |                 MANAGEMENT                |");
  39.     printf("\n\t\t\t        |                   SYSTEM                  |");
  40.     printf("\n\t\t\t        =============================================");
  41.     printf("\n\n\n\t\t\t Enter any key to continue.....");
  42.     getch();
  43. }
  44.  
  45. void mainmenu(void)
  46. {
  47.     system("COLOR 6F");
  48.     system("cls");
  49.     printf("                                       ============================================\n");
  50.     printf("                                       |           RECORD MANAGEMENT SYSTEM       | \n");
  51.     printf("                                       ============================================");
  52.     printf("\n\n\n          [1] ADD RECORDS--> \n\n          [2] VIEW RECORDS--> \n\n          [3] SEARCH RECORDS--> \n\n          [4] DELETE RECORDS--> \n\n          [5] MODIFY RECORDS-->\n\n          [6] ABOUT US--> \n\n          [7] EXIT--> \n\n\n             Enter Your Choice --->");
  53.     int choice;
  54.     scanf("%d",&choice);
  55.     //we have to keep choices here
  56.     if(choice==1)
  57.         addrecord();
  58.             if(choice==3)
  59.         searchrecord();
  60.  
  61.  
  62.         if(choice==6)
  63.         {
  64.         system("COLOR 4F");
  65.         system("cls");
  66.         printf("\n\n");
  67.         printf("        A project by:\n\n");
  68.  
  69.         printf("                         *******************************THANK YOU**************************************\n");
  70.         getch();
  71.         mainmenu();
  72.         }
  73.  
  74.  
  75.  
  76.         if(choice==7){
  77.         system("COLOR 4F");
  78.         system("cls");
  79.         printf("\n\n\n");
  80.         printf("                         *******************************THANK YOU**************************************\n");
  81.         }
  82. }
  83.     //we have to keep choices here
  84.  
  85. void addrecord()
  86. {
  87.     char back1,back2;
  88.     system("cls");
  89.     printf("                                       ============================================\n");
  90.     printf("                                       |           RECORD MANAGEMENT SYSTEM       | \n");
  91.     printf("                                       ============================================\n\n\n");
  92.     label:
  93.     ptr=fopen("C:\\Users\\Astha\\Downloads\\record.txt","a");
  94.     struct person a;
  95.     fflush(stdin);
  96.     printf("Enter name:");
  97.     gets(a.name);
  98.     fflush(stdin);
  99.     printf("Enter address:");
  100.     gets(a.address);
  101.     fflush(stdin);
  102.     printf("Enter email:");
  103.     gets(a.email);
  104.     fflush(stdin);
  105.     printf("Enter citizenship number:");
  106.     scanf("%ld",&a.citino);
  107.     printf("Enter phone:");
  108.     scanf("%ld",&a.phone);
  109.     printf("Enter date of birth in dd/mm/yy:");
  110.     scanf("%d%c%d%c%d",&a.d.dd,&back1,&a.d.mm,&back2,&a.d.yy);
  111.     fprintf(ptr,"%-30s %-20s %-30s %-20d %-15d %d/%d/%d\n",a.name,a.address,a.email,a.citino,a.phone,a.d.dd,a.d.mm,a.d.yy);
  112.     fclose(ptr);
  113.     fflush(stdin);
  114.     printf("Would you like to enter more records:(y/n)\n");
  115.     printf("Enter choice:");
  116.     fflush(stdin);
  117.     scanf("%c",&cho);
  118.     if(cho=='y'||cho=='Y')
  119.     {
  120.     system("cls");
  121.     printf("                                       ============================================\n");
  122.     printf("                                       |           RECORD MANAGEMENT SYSTEM       | \n");
  123.     printf("                                       ============================================\n\n\n");
  124.         goto label;
  125.     }
  126.     else if(cho=='n' || cho=='N')
  127.     mainmenu();
  128.  
  129. }
  130. void searchrecord()
  131. {
  132.  
  133.     system("cls");
  134.     printf("                                       ============================================\n");
  135.     printf("                                       |           RECORD MANAGEMENT SYSTEM       | \n");
  136.     printf("                                       ============================================\n\n\n");
  137.     label:
  138.     FILE*f;    
  139.    f=fopen("C:\\Users\\Astha\\Downloads\\record.txt","r");
  140.  
  141.  
  142. int flag=0;
  143. struct person e;
  144. if(f==NULL)
  145. {
  146.     printf("\n error in opening\a\a\a\a");
  147.     exit(0);
  148.  
  149. }
  150.  
  151.         char s[100];
  152.  
  153.  
  154.         printf("  Enter  Name :");
  155. scanf("%s",s);
  156.  fflush(stdin);
  157.  
  158.       while(!feof(f) && flag==0)
  159.     {
  160.  
  161.         if((strcmp(e.name,s))==0) //checks whether a.name is equal to s or not
  162.         {
  163.             flag=1;
  164.             printf("                               .........................The Record is available...........................\n\n\n");
  165.  
  166.             printf("       Name : %s\n\n",e.name);
  167.             printf("       Date OF Birth :%d/%d/%d\n\n",e.d.dd,e.d.mm,e.d.yy);
  168.              printf(" \n Address : %s",e.address);
  169.                           printf(" \n Email : %s",e.email);
  170.                           printf(" \n Citizenship number : %ld ",e.citino);
  171.                                  printf(" \n Phone : %ld",e.phone);
  172.         }
  173.     }
  174.         if (flag==0)
  175.         printf("\n aNo Record Found\a");
  176.  
  177.  
  178.  
  179.  
  180.         printf("\n\n");
  181.  
  182.  
  183.  
  184.     fclose(f);
  185.     fflush(stdin);
  186.     printf("\nWould you like to enter more records:(y/n)\n");
  187.     printf("Enter choice:");
  188.  
  189.     scanf("%c",&cho);
  190.     if(cho=='y'||cho=='Y')
  191.     {
  192.     system("cls");
  193.     printf("                                       ============================================\n");
  194.     printf("                                       |           RECORD MANAGEMENT SYSTEM       | \n");
  195.     printf("                                       ============================================\n\n\n");
  196.         goto label;
  197.     }
  198.     else if(cho=='n' || cho=='N')
  199.     mainmenu();
  200.  
  201. }
4 Weeks Ago #1
0 234

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by cwbp15 | last post: by
9 posts views Thread by =?Utf-8?B?QnJpYW4gQ29vaw==?= | last post: by
1 post views Thread by ashu0720 | last post: by
2 posts views Thread by jld730 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by kglaser89 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.