Thread: problem in the while part

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    9

    problem in the while part

    i am writing a code to modify the information in a file. the code snippet for the same is:
    Code:
    /************************MODIFY THE RECORD************************************/
    void modify(void)
    {
         char another= 'y';
         char empname[40];
         FILE *fp;
         fp= fopen("details.txt", "a");
         if(fp==0)
         {
                  printf("cannot open file");
                  exit(0);
         }
         
         while(another=='y')
         {
                          printf("\nEnter the name to be modified: ");
                          scanf("%s", empname);
                          rewind(fp);
                          while(fscanf(fp, "%s %d %f", emp.name, &emp.age, &emp.salary)!= EOF)
                          
                          {
                                    if(strcmp(emp.name, empname)==0)
                                    {
                                          printf("Enter new name: ");
                                          gets(emp.name);
                                          printf("Enter new age: ");
                                          scanf("%d", &emp.age);
                                          printf("Enter new salary: ");
                                          scanf("%f", &emp.salary);
                                          fseek(fp, -sizeof(struct _emp), SEEK_CUR);
                                          fprintf(fp, "%s\t\t\t%d\t%f", emp.name, emp.age, emp.salary);
                                    }
                                    
                          }
                          printf("no match found\n");
                          printf("\nMOdify another record (y/n): ");
                          fflush(stdin);
                          another= getche();
         }
         fclose(fp);
         return;
    }
    this code is skipping the while loop. even if i am entering the name that exists in the file details.txt, it skips the while loop and prints no match found.

    the full code of the program if required is given below

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    struct _emp
    {
           char name[40];
           int age;
           float salary;
    };
    struct _emp emp;
    
    void add(void);      //adds details to the file
    void display(void);  //display the details of the file
    void init_file(void);//initializes the file
    int menu_select(void);//displays the menu and ask for the users' choice
    void modify(void);   //modifies the record
    
    int main(void)
    {
        char choice;
        init_file(); //initialize the file
        for(;;)
        {
               system("cls");
               choice= menu_select();
               switch(choice)
               {
                             case '1': add(); break;
                             case '2': display(); break;
                             case '3': modify(); break;
                             case '4': exit(0);
                             default: printf("enter the correct choice\n"); getch(); break;
               }
        }
        return 0;
    }
    
    /****************************INITIALIZE FILE**********************************/
    void init_file(void)
    {
         FILE *fp;
         fp= fopen("details.txt", "r");
         if(fp==NULL)
         {
                     fp= fopen("details.txt", "w");
                     if(fp==NULL)
                     {
                                 printf("cannot open the file\n");
                                 getch();
                                 exit(0);
                     }
                     fprintf(fp, "Name\t\t\t\tAge\tSalary\n");
         }
         fclose(fp);
    }
    
    /*********************DISPLAYS MENU AND ASKS FOR CHOICE************************/
    int menu_select(void)
    {
        system("cls");
        char choice;
        printf("\t\t\t\tMENU.\n");
        printf("\t\t\t\t=====\n");
        printf("\t\t1. Enter the details into the file\n");
        printf("\t\t2. Display the details on the screen\n");
        printf("\t\t3. Modify\n");
        printf("\t\t4. Exit\n");
        printf("Enter the choice: ");
        choice= getche();
        return(choice);
    }
    
    /********************************ADD DETAILS TO THE FILE**********************/
    void add(void)
    {
         FILE *fp;
         char ch= 'y';
         fp= fopen("details.txt", "a");
         if(fp==NULL)
         {
                     printf("cannot open file");
                     getch();
                     exit(0);
         }
         while(ch=='y')
         {
                       printf("\nEnter name: ");
                       scanf("%s", emp.name);
                       printf("\nEnter age: ");
                       scanf("%d", &emp.age);
                       printf("\nEnter salary: ");
                       scanf("%f", &emp.salary);
                       fprintf(fp, "%s\t\t\t\t%d\t%f\t\n", emp.name, emp.age, emp.salary);
                       printf("\nEnter your choice(y/n): ");
                       fflush(stdin);
                       ch= getche();
         }
         fclose(fp);
         return;
    }
    
    /**************************DISPLAYS THE DETAILS*******************************/
    void display(void)
    {
         FILE *fp;
         fp= fopen("details.txt", "r");
         if(fp==NULL)
         {
                     printf("cannot open file");
                     getch();
                     exit(0);
         }
         fseek(fp, 20, SEEK_SET);
         printf("\n");
         printf("Name\t\t\tAge\tSalary");
         
         while (fscanf(fp, "%s %d %f", emp.name, &emp.age, &emp.salary)!= EOF)
               printf ( "\n%s\t\t\t%d\t%f", emp.name, emp.age, emp.salary ) ;
         getch();
         fclose(fp);
         return;
    }
    
    /************************MODIFY THE RECORD************************************/
    void modify(void)
    {
         char another= 'y';
         char empname[40];
         FILE *fp;
         fp= fopen("details.txt", "a");
         if(fp==0)
         {
                  printf("cannot open file");
                  exit(0);
         }
         
         while(another=='y')
         {
                          printf("\nEnter the name to be modified: ");
                          scanf("%s", empname);
                          rewind(fp);
                          while(fscanf(fp, "%s %d %f", emp.name, &emp.age, &emp.salary)!= EOF)
                          
                          {
                                    if(strcmp(emp.name, empname)==0)
                                    {
                                          printf("Enter new name: ");
                                          gets(emp.name);
                                          printf("Enter new age: ");
                                          scanf("%d", &emp.age);
                                          printf("Enter new salary: ");
                                          scanf("%f", &emp.salary);
                                          fseek(fp, -sizeof(struct _emp), SEEK_CUR);
                                          fprintf(fp, "%s\t\t\t%d\t%f", emp.name, emp.age, emp.salary);
                                    }
                                    
                          }
                          printf("no match found\n");
                          printf("\nMOdify another record (y/n): ");
                          fflush(stdin);
                          another= getche();
         }
         fclose(fp);
         return;
    }

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Well, from you code it looks like it will always print "no match found" since there itsn't any way to skip that line of code.

    I suggest you print out the name you are looking for, and each name that you read from the file while you are searching through it. Ideally this print would go before the strcmp line.
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    9
    i didnt follow you.
    i know that the line: no match found will be printed in any case, but the problem here is that while part is never effected , even if the name exists in the details.txt.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    What I mean is to change your while loop so it looks like this:
    Code:
    while(fscanf(fp, "%s %d %f", emp.name, &emp.age, &emp.salary)!= EOF)
    {
        printf("Looking for %s, found: %s\n", empname, emp.name);
        if(strcmp(emp.name, empname)==0)
        {   
            printf("Enter new name: ");
            gets(emp.name);
            printf("Enter new age: ");
            scanf("%d", &emp.age);
            printf("Enter new salary: ");
            scanf("%f", &emp.salary);
            fseek(fp, -sizeof(struct _emp), SEEK_CUR);
            fprintf(fp, "%s\t\t\t%d\t%f", emp.name, emp.age, emp.salary);
        }   
    }
    bit∙hub [bit-huhb] n. A source and destination for information.

  5. #5
    Registered User
    Join Date
    Sep 2010
    Posts
    9
    yeah this will definitely remove the continuous printing of the "name not found statement", but the success of name comparison is not achieved.

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote Originally Posted by abhishekabby View Post
    yeah this will definitely remove the continuous printing of the "name not found statement", but the success of name comparison is not achieved.
    Why is it not achieved? What is getting printed out? The point of adding the print statement is to help you diagnose the problem, not to fix any specific problem.
    bit∙hub [bit-huhb] n. A source and destination for information.

  7. #7
    Registered User
    Join Date
    Sep 2010
    Posts
    9
    the if condition: if(strcmp(emp.name, empname)==0) is not satisfied in this code.
    i have tried the same code with a binary file with DAT extension then the code works.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting illegal case error
    By scmurphy64 in forum C Programming
    Replies: 2
    Last Post: 09-17-2009, 10:35 AM
  2. file handling and function problem.
    By prepare in forum C Programming
    Replies: 7
    Last Post: 10-09-2004, 02:26 AM
  3. !!!Urgent Help on a group project!!!!!
    By AmeenR in forum C Programming
    Replies: 3
    Last Post: 12-13-2003, 09:22 PM
  4. Memory Problem - I think...
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 10-24-2001, 12:14 PM