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