Thread: inappropriate output while readin the content of a file

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

    inappropriate output while readin the content of a file

    i wrote a program which performs the following functions:
    1> read the content.
    2> display the content.

    in my second part, the out put i am getting is not appropriate. my code snippet for the display function:

    Code:
    void display(void)
    {
         FILE *fp;
         fp= fopen("details.txt", "r");
         if(fp==NULL)
         {
                     printf("cannot open file");
                     getch();
                     exit(0);
         }
         rewind(fp);
         printf("\n");
         while(fread(&emp, sizeof(struct _emp), 1, fp)==1)
                          printf("%s\t\t\t%d\t%f", emp.name, emp.age, emp.salary);
         getch();
         fclose(fp);
         return;
    }

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Are we supposed to know what is appropriate versus inappropriate? Where is emp declared? How is struct _emp declared?

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is inappropriate about the output? What is emp (i.e., is it really a struct _emp)? What is a struct emp_, anyway?

    You do not need to use rewind here too.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Sep 2010
    Posts
    9
    this is just the snippet.
    the inappropriate here means:
    if the file contains values:

    name age salary
    abby 23 27000
    ashi 22 12345

    then the output i am getting is:

    name age salary
    abby 23 27000.000000
    ashi,*, 4320 1236457584382635.000000

    m posting my entire code:
    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
    
    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': 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. 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);
         }
         rewind(fp);
         printf("\n");
         while(fread(&emp, sizeof(struct _emp), 1, fp)==1)
                          printf("%s\t\t\t%d\t%f", emp.name, emp.age, emp.salary);
         getch();
         fclose(fp);
         return;
    }

  5. #5
    Registered User
    Join Date
    Sep 2010
    Posts
    9
    this is my actual output.



    MENU.
    =====
    1. Enter the details into the file
    2. Display the details on the screen
    3. Exit
    Enter the choice: 2
    Name Age Salary
    abby 23 27000.000000
    ashi,☼, 168374320 17563478226893695000000000.000000

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Code:
    fflush(stdin);
    Undefined behavior. Bad.

    You're creating the file as a text file, opening it for reading as a text file, then reading it as if it were a binary file.

  7. #7
    Registered User
    Join Date
    Sep 2010
    Posts
    9
    how am i suppose to correct it??

  8. #8
    Registered User
    Join Date
    Sep 2010
    Posts
    9
    hey i got the correct output. i changed the code from:
    Code:
    while(fread(&emp, sizeof(struct _emp), 1, fp)==1)
                          printf("%s\t\t\t%d\t%f", emp.name, emp.age, emp.salary);
    to

    Code:
    while ( fscanf ( fp, "%s\t\t\t%d\t%f", emp.name, &emp.age, &emp.salary ) != EOF )
               printf ( "\n%s\t\t\t%d\t%f", emp.name, emp.age, emp.salary ) ;
    thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. IDE for C embedded advanced editing
    By Undici77 in forum Tech Board
    Replies: 32
    Last Post: 01-16-2010, 05:17 PM
  3. Copying content of file into a 2D array
    By trueman1991 in forum C Programming
    Replies: 10
    Last Post: 12-16-2009, 12:42 AM
  4. Need help fixing bugs in data parsing program
    By daluu in forum C Programming
    Replies: 8
    Last Post: 03-27-2003, 06:02 PM