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

J

JackC

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
 
V

Victor Bazarov

JackC said:
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
 
J

JackC

JackC said:
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

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
 
V

Victor Bazarov

JackC said:
JackC said:
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

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
 
J

JackC

JackC said:
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
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

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.
 
V

Victor Bazarov

JackC said:
[..] 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.
 
J

James Kanze

JackC said:
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 ) ;
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top