Can't find the BUG

This is a discussion on Can't find the BUG within the C Programming forums, part of the General Programming Boards category; This is a part of a code i wrote, i BOLD-marked the problem, an anyone fine it? Code: index = ...

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    1

    Can't find the BUG

    This is a part of a code i wrote, i BOLD-marked the problem,
    an anyone fine it?

    Code:
    index = ((id%MAX_BORROW_SIZE)*(sizeof(
    brw_movie)));
       
        fseek(fp_borrowed, index, SEEK_SET);
        fread(&temp, sizeof(brw_movie), 1, fp_borrowed);
       
        if (temp.ordered == 1)
        {
            temp.ordered = 0;
            fseek(fp_borrowed, index, SEEK_SET);
    
            //temp.catalog_id = 0;
            fwrite(&temp, sizeof(brw_movie), 1, fp_borrowed);
            printf("Movie id# %d has been deleted from Borrowed Movies.\n", id);
    
            // **************** CHECK *************** //
    
            fseek(fp_borrowed, index, SEEK_SET);
            fread(&temp, sizeof(brw_movie), 1, fp_borrowed);
            printf(" --> %d\n", temp.ordered);

    OUTPUT -->

    What is the movie catalog no#? 22
    Your fine is: 75.000000 due to lag of 15 days!
    Movie id# 22 has been deleted from Borrowed Movies.
    --> 1
    The movie was already ordered by another customer (# 2).
    Last edited by Salem; 08-10-2008 at 04:46 AM. Reason: Added code tags - learn to use them, like you did with bold

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,920
    Tried flushing the file after writing and before reading?
    Oh and do use code tags.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    Tried flushing the file after writing and before reading?
    Oh and do use code tags.
    Flushing should not be necessary - the filesystem should be consistent in that sense.

    One suggestion (as I can't see anything directly wrong with the code itself) it would probably be a good idea to check the results from fseek, fread and fwrite. If you get errors, check that the file is opened in "r+" mode, perhaps?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,767
    I would have suggested fflush(), but fseek() seems good
    Quote Originally Posted by c99 on fopen
    When a file is opened with update mode (+ as the second or third character in the
    above list of mode argument values), both input and output may be performed on the
    associated stream. However, output shall not be directly followed by input without an
    intervening call to the fflush function or to a file positioning function (fseek,
    fsetpos, or rewind), and input shall not be directly followed by output without an
    intervening call to a file positioning function, unless the input operation encounters endof-
    file. Opening (or creating) a text file with update mode may instead open (or create) a
    binary stream in some implementations.
    The next step is definitely improving the error checking.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,920
    Quote Originally Posted by matsp View Post
    Flushing should not be necessary - the filesystem should be consistent in that sense.
    I looked at msdn, but couldn't find any guarantees of fseek flushing the stream before seeking. But then again, MSDN is not known to be the best source.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ATL bug of CComPtr?
    By George2 in forum Windows Programming
    Replies: 6
    Last Post: 04-07-2008, 08:52 AM
  2. Can't Find Bug in basic MP3 Sorter
    By trickeastcs in forum C++ Programming
    Replies: 12
    Last Post: 12-14-2007, 05:31 PM
  3. Replies: 5
    Last Post: 04-16-2004, 02:29 AM
  4. Time for another round of: FIND THAT BUG!
    By DeepBlackMagic in forum C++ Programming
    Replies: 0
    Last Post: 10-29-2003, 05:29 AM
  5. linked list recursive function spaghetti
    By ... in forum C++ Programming
    Replies: 4
    Last Post: 09-02-2003, 03:53 PM

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