472,146 Members | 1,373 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,146 software developers and data experts.

C text file for storing and searching file with name

I made a program which adds , deletes and searches the contacts on the basis of their names. the add works file but the search function says "no record found" even if i enter a name which is present in the text file. am i wrong with the while syntax? can anyone help me fix the code? ive searched books and web the whole day and i still cant seem to debug it. any help would be much appreciated. the search records starts at line 131 . thank you!
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("\n\n");
  70.         printf("                         *******************************THANK YOU**************************************\n");
  71.         getch();
  72.         mainmenu();
  73.         }
  74.  
  75.  
  76.  
  77.         if(choice==7){
  78.         system("COLOR 4F");
  79.         system("cls");
  80.         printf("\n\n\n");
  81.         printf("                         *******************************THANK YOU**************************************\n");
  82.         }
  83. }
  84.     //we have to keep choices here
  85.  
  86. void addrecord()
  87. {
  88.     char back1,back2;
  89.     system("cls");
  90.     printf("                                       ============================================\n");
  91.     printf("                                       |           RECORD MANAGEMENT SYSTEM       | \n");
  92.     printf("                                       ============================================\n\n\n");
  93.     label:
  94.     ptr=fopen("C:\\Users\\Astha\\Downloads\\record.txt","a");
  95.     struct person a;
  96.     fflush(stdin);
  97.     printf("Enter name:");
  98.     gets(a.name);
  99.     fflush(stdin);
  100.     printf("Enter address:");
  101.     gets(a.address);
  102.     fflush(stdin);
  103.     printf("Enter email:");
  104.     gets(a.email);
  105.     fflush(stdin);
  106.     printf("Enter citizenship number:");
  107.     scanf("%ld",&a.citino);
  108.     printf("Enter phone:");
  109.     scanf("%ld",&a.phone);
  110.     printf("Enter date of birth in dd/mm/yy:");
  111.     scanf("%d%c%d%c%d",&a.d.dd,&back1,&a.d.mm,&back2,&a.d.yy);
  112.     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);
  113.     fclose(ptr);
  114.     fflush(stdin);
  115.     printf("Would you like to enter more records:(y/n)\n");
  116.     printf("Enter choice:");
  117.     fflush(stdin);
  118.     scanf("%c",&cho);
  119.     if(cho=='y'||cho=='Y')
  120.     {
  121.     system("cls");
  122.     printf("                                       ============================================\n");
  123.     printf("                                       |           RECORD MANAGEMENT SYSTEM       | \n");
  124.     printf("                                       ============================================\n\n\n");
  125.         goto label;
  126.     }
  127.     else if(cho=='n' || cho=='N')
  128.     mainmenu();
  129.  
  130. }
  131. void searchrecord()
  132. {
  133.  
  134.     system("cls");
  135.     printf("                                       ============================================\n");
  136.     printf("                                       |           RECORD MANAGEMENT SYSTEM       | \n");
  137.     printf("                                       ============================================\n\n\n");
  138.     label:
  139.     FILE*f;    
  140.    f=fopen("C:\\Users\\Astha\\Downloads\\record.txt","r");
  141.  
  142.  
  143. int flag=0;
  144. struct person e;
  145. if(f==NULL)
  146. {
  147.     printf("\n error in opening\a\a\a\a");
  148.     exit(0);
  149.  
  150. }
  151.  
  152.         char s[100];
  153.  
  154.  
  155.         printf("  Enter  Name :");
  156. scanf("%s",s);
  157.  fflush(stdin);
  158.  
  159.       while(!feof(f) && flag==0)
  160.     {
  161.  
  162.         if((strcmp(e.name,s))==0) //checks whether a.name is equal to s or not
  163.         {
  164.             flag=1;
  165.             printf("                               .........................The Record is available...........................\n\n\n");
  166.  
  167.             printf("       Name : %s\n\n",e.name);
  168.             printf("       Date OF Birth :%d/%d/%d\n\n",e.d.dd,e.d.mm,e.d.yy);
  169.              printf(" \n Address : %s",e.address);
  170.                           printf(" \n Email : %s",e.email);
  171.                           printf(" \n Citizenship number : %ld ",e.citino);
  172.                                  printf(" \n Phone : %ld",e.phone);
  173.         }
  174.     }
  175.         if (flag==0)
  176.         printf("\n aNo Record Found\a");
  177.  
  178.  
  179.  
  180.  
  181.         printf("\n\n");
  182.  
  183.  
  184.  
  185.     fclose(f);
  186.     fflush(stdin);
  187.     printf("\nWould you like to enter more records:(y/n)\n");
  188.     printf("Enter choice:");
  189.  
  190.     scanf("%c",&cho);
  191.     if(cho=='y'||cho=='Y')
  192.     {
  193.     system("cls");
  194.     printf("                                       ============================================\n");
  195.     printf("                                       |           RECORD MANAGEMENT SYSTEM       | \n");
  196.     printf("                                       ============================================\n\n\n");
  197.         goto label;
  198.     }
  199.     else if(cho=='n' || cho=='N')
  200.     mainmenu();
  201.  
  202. }
  203.  
  204.  
Aug 17 '21 #1
1 2501
dev7060
624 Expert 512MB
I made a program which adds , deletes and searches the contacts on the basis of their names. the add works file but the search function says "no record found" even if i enter a name which is present in the text file. am i wrong with the while syntax? can anyone help me fix the code? ive searched books and web the whole day and i still cant seem to debug it. any help would be much appreciated. the search records starts at line 131 . thank you!
Expand|Select|Wrap|Line Numbers
  1. void searchrecord()
  2. {
  3.     system("cls");
  4.     printf("                                       ============================================\n");
  5.     printf("                                       |           RECORD MANAGEMENT SYSTEM       | \n");
  6.     printf("                                       ============================================\n\n\n");
  7.     label:
  8.         FILE * f;
  9.     f = fopen("C:\\Users\\Astha\\Downloads\\record.txt", "r");
  10.  
  11.     int flag = 0;
  12.     struct person e;
  13.     if (f == NULL)
  14.     {
  15.         printf("\n error in opening\a\a\a\a");
  16.         exit(0);
  17.  
  18.     }
  19.  
  20.     char s[100];
  21.  
  22.     printf("  Enter  Name :");
  23.     scanf("%s", s);
  24.     fflush(stdin);
  25.  
  26.     while (!feof(f) && flag == 0)
  27.     {
  28.         if ((strcmp(e.name, s)) == 0)    //checks whether a.name is equal to s or not
  29.         {
  30.             flag = 1;
  31.             printf("                               .........................The Record is available...........................\n\n\n");
  32.  
  33.             printf("       Name : %s\n\n", e.name);
  34.             printf("       Date OF Birth :%d/%d/%d\n\n", e.d.dd, e.d.mm, e.d.yy);
  35.             printf(" \n Address : %s", e.address);
  36.             printf(" \n Email : %s", e.email);
  37.             printf(" \n Citizenship number : %ld ", e.citino);
  38.             printf(" \n Phone : %ld", e.phone);
  39.         }
  40.     }
  41.  
  42.     if (flag == 0)
  43.         printf("\n aNo Record Found\a");
  44.  
  45.     printf("\n\n");
  46.  
  47.     fclose(f);
  48.     fflush(stdin);
  49.     printf("\nWould you like to enter more records:(y/n)\n");
  50.     printf("Enter choice:");
  51.  
  52.     scanf("%c", &cho);
  53.     if (cho == 'y' || cho == 'Y')
  54.     {
  55.         system("cls");
  56.         printf("                                       ============================================\n");
  57.         printf("                                       |           RECORD MANAGEMENT SYSTEM       | \n");
  58.         printf("                                       ============================================\n\n\n");
  59.         goto label;
  60.     }
  61.     else if (cho == 'n' || cho == 'N')
  62.         mainmenu();
  63.  
  64. }
I see no code that reads the data from the file to the structure variable.
Aug 22 '21 #2

Post your reply

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

Similar topics

6 posts views Thread by Rizwan | last post: by
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
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.