File Space Allocation + Writing (C++/Linux)

Discussion in 'C++' started by JackC, Aug 7, 2007.

  1. JackC

    JackC Guest

    Hi,

    My program takes a series of encoded file segments, and decodes each
    segment and writes it to the output file.

    I am trying to approach this as follows:

    I know the size of the output file in bytes so i pre-allocate the
    space for it and fill it with NULL bytes using the linux truncate
    function, I do this incase one of segments is missing i just want null
    bytes in its place with the other segments around the missing block.

    I also know the position (start and end) in bytes where each decoded
    segment needs to go in the output file, so using ofstream i am
    'attempting' to use seekp to set the file pointer at the position on
    the truncated file and write my decoded segment out.

    The problem is that if I don't open the file in append mode (ios::app)
    then it automatically overwrites the output file contents, so all
    previous written segments are deleted. And if i use append mode, then
    seekp doesn't seem to work, and after the truncate() is performed, the
    segments just get appended onto the end of the file instead of
    overwriting the null bytes at the position specified.

    Now for some code:

    ofstream ostr(ofile.c_str(), ios::app); // Open output file in append
    mode
    ....
    truncate(ofile.c_str(), file_size_in_bytes); // Fill the file to
    specified size with NULL bytes
    ....
    ostr.seekp(begin_bytes - 1);
    // Move to segment start position in the file
    ....

    My decoding/file output

    //Clean up
    ostr.close();



    Anyone know where im going wrong? If i call ostr.tellp() it suggests
    im at the correct place in the file, but when i actually write stuff
    out with ostr.put() it goes at the wrong place in the file

    Thanks
    JackC, Aug 7, 2007
    #1
    1. Advertising

  2. JackC wrote:
    > My program takes a series of encoded file segments, and decodes each
    > segment and writes it to the output file.
    > [..]
    > The problem is that if I don't open the file in append mode (ios::app)
    > then it automatically overwrites the output file contents, so all
    > previous written segments are deleted. And if i use append mode, then
    > seekp doesn't seem to work, and after the truncate() is performed, the
    > segments just get appended onto the end of the file instead of
    > overwriting the null bytes at the position specified.


    Have you tried opening in read+write mode?

    > [..]


    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Aug 7, 2007
    #2
    1. Advertising

  3. JackC

    JackC Guest

    On 7 Aug, 20:45, "Victor Bazarov" <> wrote:
    > JackC wrote:
    > > My program takes a series of encoded file segments, and decodes each
    > > segment and writes it to the output file.
    > > [..]
    > > The problem is that if I don't open the file in append mode (ios::app)
    > > then it automatically overwrites the output file contents, so all
    > > previous written segments are deleted. And if i use append mode, then
    > > seekp doesn't seem to work, and after the truncate() is performed, the
    > > segments just get appended onto the end of the file instead of
    > > overwriting the null bytes at the position specified.

    >
    > Have you tried opening in read+write mode?
    >
    > > [..]

    >
    > V
    > --
    > Please remove capital 'A's when replying by e-mail
    > I do not respond to top-posted replies, please don't ask


    Thanks, How exactly would i do that with ofstream? I have tried using
    ios::eek:ut but with no success is that the mode you meant?

    Cheers,
    Jack
    JackC, Aug 7, 2007
    #3
  4. JackC wrote:
    > On 7 Aug, 20:45, "Victor Bazarov" <> wrote:
    >> JackC wrote:
    >>> My program takes a series of encoded file segments, and decodes each
    >>> segment and writes it to the output file.
    >>> [..]
    >>> The problem is that if I don't open the file in append mode
    >>> (ios::app) then it automatically overwrites the output file
    >>> contents, so all previous written segments are deleted. And if i
    >>> use append mode, then seekp doesn't seem to work, and after the
    >>> truncate() is performed, the segments just get appended onto the
    >>> end of the file instead of overwriting the null bytes at the
    >>> position specified.

    >>
    >> Have you tried opening in read+write mode?
    >>
    >>> [..]

    >>
    >> V
    >> --
    >> Please remove capital 'A's when replying by e-mail
    >> I do not respond to top-posted replies, please don't ask

    >
    > Thanks, How exactly would i do that with ofstream? I have tried using
    > ios::eek:ut but with no success is that the mode you meant?


    Obviously you can't do that with an *o*fstream. Use std::fstream.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Aug 7, 2007
    #4
  5. JackC

    JackC Guest

    On 7 Aug, 21:32, "Victor Bazarov" <> wrote:
    > JackC wrote:
    > > On 7 Aug, 20:45, "Victor Bazarov" <> wrote:
    > >> JackC wrote:
    > >>> My program takes a series of encoded file segments, and decodes each
    > >>> segment and writes it to the output file.
    > >>> [..]
    > >>> The problem is that if I don't open the file in append mode
    > >>> (ios::app) then it automatically overwrites the output file
    > >>> contents, so all previous written segments are deleted. And if i
    > >>> use append mode, then seekp doesn't seem to work, and after the
    > >>> truncate() is performed, the segments just get appended onto the
    > >>> end of the file instead of overwriting the null bytes at the
    > >>> position specified.

    >
    > >> Have you tried opening in read+write mode?

    >
    > >>> [..]

    >
    > >> V
    > >> --
    > >> Please remove capital 'A's when replying by e-mail
    > >> I do not respond to top-posted replies, please don't ask

    >
    > > Thanks, How exactly would i do that with ofstream? I have tried using
    > > ios::eek:ut but with no success is that the mode you meant?

    >
    > Obviously you can't do that with an *o*fstream. Use std::fstream.
    >
    > V
    > --
    > Please remove capital 'A's when replying by e-mail
    > I do not respond to top-posted replies, please don't ask


    Thanks, i couldn't get it working with fstream, but i switched to the
    native linux file I/O functions i.e open(), write() etc and everything
    works fine so i might as well stick with these as portability isn't an
    issue.
    JackC, Aug 7, 2007
    #5
  6. JackC wrote:
    > [..] i couldn't get it working with fstream, but i switched to the
    > native linux file I/O functions i.e open(), write() etc and everything
    > works fine so i might as well stick with these as portability isn't an
    > issue.


    Whatever works for you. Sorry I couldn't help.
    Victor Bazarov, Aug 7, 2007
    #6
  7. JackC

    James Kanze Guest

    On Aug 7, 10:32 pm, "Victor Bazarov" <> wrote:
    > JackC wrote:
    > > On 7 Aug, 20:45, "Victor Bazarov" <> wrote:
    > >> JackC wrote:
    > >>> My program takes a series of encoded file segments, and decodes each
    > >>> segment and writes it to the output file.
    > >>> [..]
    > >>> The problem is that if I don't open the file in append mode
    > >>> (ios::app) then it automatically overwrites the output file
    > >>> contents, so all previous written segments are deleted. And if i
    > >>> use append mode, then seekp doesn't seem to work, and after the
    > >>> truncate() is performed, the segments just get appended onto the
    > >>> end of the file instead of overwriting the null bytes at the
    > >>> position specified.


    > >> Have you tried opening in read+write mode?


    > >>> [..]


    > > Thanks, How exactly would i do that with ofstream? I have tried using
    > > ios::eek:ut but with no success is that the mode you meant?


    > Obviously you can't do that with an *o*fstream.


    Why not? According to the standard:

    std::eek:fstream f( filename, std::ios::in ) ;

    is supposed to work. In such cases, I'd explicitly or in
    std::ios::eek:ut as well, just to be clearer, even though
    ofstream::eek:pen does it for you. In fact, I'd probably add some
    sort of comment to the effect that I was doing this because it
    is the only way to get an update mode, and not because I wanted
    to read.

    Also, of course, seeking to arbitrary positions only works if
    the file is opened in binary. So the final declaration would
    actually be:

    std::eek:fstream f( filename,
    std::ios::eek:ut | std::ios::in |
    std::ios::binary ) ;

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Aug 8, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Shuo Xiang

    Stack space, global space, heap space

    Shuo Xiang, Jul 9, 2003, in forum: C Programming
    Replies:
    10
    Views:
    2,885
    Bryan Bullard
    Jul 11, 2003
  2. Christian Seberino
    Replies:
    21
    Views:
    1,649
    Stephen Horne
    Oct 27, 2003
  3. Ian Bicking
    Replies:
    2
    Views:
    1,006
    Steve Lamb
    Oct 23, 2003
  4. Ian Bicking
    Replies:
    2
    Views:
    717
    Michael Hudson
    Oct 24, 2003
  5. Ken
    Replies:
    24
    Views:
    3,856
    Ben Bacarisse
    Nov 30, 2006
Loading...

Share This Page