negative offset

Discussion in 'C' started by markyjj, Dec 8, 2005.

  1. markyjj

    markyjj New Member

    Joined:
    Dec 7, 2005
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    0
    Could someone please help me with the following code:

    Inout_file.seekp(((long)-1 * sizeof(int)), ios::cur);
    // offset // // mode//

    This code is used to move the offset back 4 bytes from the 12th byte after replacing a value in the list. The above offset is -4 in order to move the offset back and this results in the offset now being at the 8th byte. I cant understand why the offset is -4 though. Is this to do with the ‘long’ and what is the -1 for?. Also this code is worked on the basis that integers are 4 bytes.

    Hope that anyone can help…thanks
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    The seekp member function sets this pointer and thus provides random-access disk file output.

    Now about the code You are passing the first parameter as
    -1 * sizeof(int)
    Which is
    -1 * 4 [As you have told integer is 4 bytes]
    And so the result becomes
    -4 and so seekp moves the pointer 4 bytes in the reverse direction and so it moves from 12th to 8th byte.

    Thanks
    Shabbir Bhimani
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice