473,327 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 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 2775
dev7060
633 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

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

Similar topics

6
by: Rizwan | last post by:
I have the following tasks : 1) receive a flat text file which contains data and 2) convert it into an XML The flat text file for example is in this format : 1 2...
5
by: cwbp15 | last post by:
Using Visual Studio C# When I ran the following code: System.IO; private void Button1_Click(object sender, System.EventArgs e) { //FileStream fs = File.Create(Server.MapPath("test.txt"));...
3
by: C# to XML | last post by:
Hi Friends, I am new to csharp , I am using the following code to write into xml file : Just go through the code : fp = File.OpenText(Server.MapPath(".\\upload\\") + "test.txt"); string...
2
by: ahynes | last post by:
HI folks, I'm a chem engineer with no VB programming knowledge (as you'll see from my question!) I want a script to open a .txt file, insert pre-defined text into the start and end of the...
9
by: =?Utf-8?B?QnJpYW4gQ29vaw==?= | last post by:
I want to open a text file and format it into a specific line and then apply color to a specific location of the text and then display it in a RichTextBox after all of this is done. I can do all...
1
by: ashu0720 | last post by:
how to convert text file into image file in VC++
2
by: jld730 | last post by:
Greetings! I am still new to Python, sorry! I have been searching through many posts on this subject and have attempted to TRY, but I feel really lost. So, any detailed guidance would be oh-so...
2
by: progvar | last post by:
Hi! can any one help me by providing the method when i open any text file and convert into pdf format. I searched on the net and i got some code but i am not understanding this code and it also...
1
by: agarwalsunitadhn | last post by:
Hi I am developing an application in which i need to convert a text file into a resource file and then serach different resources from the resource file. I want to know how to create the resource...
13
by: madankarmukta | last post by:
Hi all, Is there any way by which I can export the data from the dataset table to the text file or xls file ? Currently I implemented this by itterating through each row of the table and...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.