By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,986 Members | 1,944 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,986 IT Pros & Developers. It's quick & easy.

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

P: n/a
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

Aug 7 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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
Aug 7 '07 #2

P: n/a
On 7 Aug, 20:45, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
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::out but with no success is that the mode you meant?

Cheers,
Jack

Aug 7 '07 #3

P: n/a
JackC wrote:
On 7 Aug, 20:45, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>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::out 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
Aug 7 '07 #4

P: n/a
On 7 Aug, 21:32, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
JackC wrote:
On 7 Aug, 20:45, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
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::out 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.

Aug 7 '07 #5

P: n/a
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.
Aug 7 '07 #6

P: n/a
On Aug 7, 10:32 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
JackC wrote:
On 7 Aug, 20:45, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
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::out 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::ofstream f( filename, std::ios::in ) ;

is supposed to work. In such cases, I'd explicitly or in
std::ios::out as well, just to be clearer, even though
ofstream::open 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::ofstream f( filename,
std::ios::out | std::ios::in |
std::ios::binary ) ;

--
James Kanze (GABI Software) email:ja*********@gmail.com
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

Aug 8 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.