Closed Thread
Results 1 to 6 of 6

Thread: fscanf returns incorrect values in C

  1. #1
    CheapFungus Guest

    fscanf returns incorrect values in C

    Hi! I've been having problems regarding my file reading. I've noticed that it returns a weird value, (it returns a 0.00) and stores this incorrect value in the structure.I've noticed that this happens usually if the file is empty. Here's the part of my code that deals with the reading of the file. I've initialized the fp in another part of the program, by the way.
    I'd aprreciate all your help
    Code:
    int reads ()
    {
        
        int i= 0, j;
        char c;
        
        fp = fopen("dishes.txt", "r");  
        
        
        
        if (fp != NULL)
        {
           while (!feof(fp))
           {
              fscanf (fp,"%50s %50s %f", dish[i].name, dish[i].describe, &dish[i].price);
              dishtotal++;          
              i++;
           }//while loop
           
        }//if
       // printf ("%d", i);
        
        fclose(fp);
        getch();
        return;
    }


    The format of the file looks like this:


    string1 string2 price(in float)
    sundae chocolate 5.00
    lasagna italian 50.00

    and so on...
    Last edited by ZekeDragon; 03-21-2010 at 01:39 AM. Reason: Please use [code] tags (the # button) when posting code.

  2. CODECALL Circuit advertisement

     
  3. #2
    Join Date
    Jul 2009
    Location
    Santa Clarita, CA
    Posts
    2,111
    Blog Entries
    47
    Rep Power
    31

    Re: fscanf returns incorrect values in C

    Could you give us an example file that you open which causes the problem? You said it usually happens when the file is empty... which really shouldn't be that surprising.
    Wow I changed my sig!

  4. #3
    CheapFungus Guest

    Re: fscanf returns incorrect values in C

    Say I have a file that contains these:

    Lasagna Italian Dish 10.00
    Sundae Chocolate 9.00
    Nachos Mexican 5.00


    My program has a delete function which deletes the items in the file, so there are really instances that the file must be empty. I don't want it to return weird values because it kinda mucks up the program , so there

  5. #4
    Join Date
    Sep 2009
    Location
    USA
    Posts
    3,400
    Blog Entries
    5
    Rep Power
    37

    Re: fscanf returns incorrect values in C

    Maybe this will work:
    Code:
    if (dish[i].price==0) {
        printf("File is empty!\n");
        fclose(fp);
        return 1;
    }
    If it returns the weird value, say the file is empty and stop the program.
    Root Beer == System Administrator's Beer
    Download the new operating system programming kit! (some assembly required)

  6. #5
    farajnew is offline Newbie
    Join Date
    Mar 2010
    Posts
    2
    Rep Power
    0

    Re: fscanf returns incorrect values in C

    Quote Originally Posted by CheapFungus View Post
    fscanf (fp,"%50s %50s %f", dish[i].name, dish[i].describe, &dish[i].price);

    } [/code]



    .
    Maybe the problem could be with using %50s
    try without the "50" , since the largest string in English is 22 char and u r using 50 char to get string

    regards
    FK

  7. #6
    dcs
    dcs is offline Guru
    Join Date
    Mar 2008
    Posts
    775
    Rep Power
    23

    Re: fscanf returns incorrect values in C

    Avoid loop control using EOF. Instead, check the return value of fscanf for correctness.

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. MYSQL: Incorrect Counts
    By BASHERS33 in forum Database & Database Programming
    Replies: 9
    Last Post: 07-12-2009, 03:25 PM
  2. fscanf question
    By boredaxel in forum C and C++
    Replies: 2
    Last Post: 11-09-2008, 06:44 PM
  3. [BUG] Version value is incorrect
    By rolandd in forum ionFiles
    Replies: 4
    Last Post: 10-30-2007, 04:34 AM
  4. [BUG] Apply behaviour incorrect
    By rolandd in forum ionFiles
    Replies: 1
    Last Post: 10-14-2007, 08:00 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts