473,327 Members | 1,979 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

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. }
Aug 17 '21 #1
0 945

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

Similar topics

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...
2
by: pulavarthipraveen | last post by:
Overview: We have a requirement in the c#.NET 1.0 windows application. There will be some input text file in the user’s machine. The user should browse and select the input text file and also select...
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
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...
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....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.