problem with reading from file.

This is a discussion on problem with reading from file. within the C Programming forums, part of the General Programming Boards category; m trying to read records for a library database from a binary file. Code: while (fread(&lib, recsize,1,fp) == 1) { ...

  1. #1
    esi
    esi is offline
    Registered User
    Join Date
    Nov 2006
    Posts
    18

    problem with reading from file.

    m trying to read records for a library database from a binary file.
    Code:
    while (fread(&lib, recsize,1,fp) == 1)
    			{	
                                    gotoxy (20,10);
    				printf("Title:");
    				gotoxy(28,10);
    				printf("%s",lib.title);
    				gotoxy (20,12);
    				printf("Author:");
    				gotoxy(28,12);
    				printf("%s",lib.author);
    				gotoxy (20,14);
    				printf("ISBN:");
    				gotoxy(28,14);
    				printf("%d",lib.isbn);
    				gotoxy (20,16);
    				printf("S.NO:");
    				gotoxy(28,16);
    				printf("%d",lib.sno);
                            }

    my program compiles fine but when i run the function for display records. it justs skips this part. it does not enter the while loop at all. what am i doing wrong over here?
    thank you

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    fread() returns the size in bytes that have been read, I believe. If recsize is not 1, that's your problem. Alter what you're comparing the return value of fread() to, from 1 to (recsize*1) or simply just recsize (which makes more sense).

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Obviously fread() is encountering an error of some sort . . .

    Post the code the opens the file and any code that accesses the file before this while loop. The file could be opened incorrectly. All of the data from the file could have already been read in, which could be fixed by a call to rewind(). The file could be empty. There are lots of possibilities.

    [edit]
    fread() returns the size in bytes that have been read, I believe. If recsize is not 1, that's your problem. Alter what you're comparing the return value of fread() to, from 1 to (recsize*1) or simply just recsize (which makes more sense).
    That's what I thought too, until I looked it up:
    Return Value
    The total number of elements successfully read is returned as a size_t object, which is an integral data type.
    If this number differs from the count parameter, either an error occured or the End Of File was reached.
    You can use either ferror or feof to check whether an error happened or the End-of-File was reached.
    From http://www.cplusplus.com/reference/c...dio/fread.html

    So the return value should be compared with the count parameter, which is in this case 1.
    Code:
    size_t fread(void *ptr, size_t size, size_t nitems, FILE *stream);
    That reference isn't wrong, either, which crossed my mind. Here's another source. http://www.opengroup.org/pubs/online...xsh/fread.html
    RETURN VALUE
    Upon successful completion, fread() returns the number of members successfully read which is less than nitems only if a read error or end-of-file is encountered. If size or nitems is 0, fread() returns 0 and the contents of the array and the state of the stream remain unchanged. Otherwise, if a read error occurs, the error indicator for the stream is set and errno is set to indicate the error.
    [/edit]
    Last edited by dwks; 06-23-2007 at 02:40 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Bleh.... I should have looked it up.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    If you think about it, it makes sense. You wouldn't want to have an overflow in the return value just because size*nitems couldn't fit into a size_t.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Yeah, you're right. That is the logical way to handle it in that regard. That made me realize.... what if you read in half a record? You'd have to handle deciding what to do with a partial record, as opposed to fread() only returning complete records.

    OK, that's enough. Stop making me feel stupid. lol...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  3. Replies: 20
    Last Post: 06-12-2005, 11:53 PM
  4. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  5. Problem reading file
    By winsonlee in forum C Programming
    Replies: 2
    Last Post: 04-23-2004, 06:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21