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

File full of '^@' characters after binary write

P: n/a
I'm running into a strange problem with ostreams's write function.
Please see the code below:

ofstream save((job->b->get_filename()).c_str(), ios::out |
ios::binary);
if (!save) {
cout << "Cannot open file" << endl;
return;
}
else {
if (job->b->get_offset() != 0)
save.seekp((job->b->get_offset() * BLOCK_SIZE)+1, ios::beg);
save.write(job->b->get_data(), (sizeof(char) *
job->b->get_size()));
cout << "Seek left the put pointer at byte " << save.tellp()
<<endl;
}
save.close()

This is just the snippit out of the middle of a file, but the idea is
pretty strait forward. I want to be able to make random reads and
writes into a binary file. If I write data to this file, close it,
open it, then seek past eof, it is my understanding that a write will
pad out any empty space with 0's.

Well, I'm writing a file in sequential blocks, and only the last block
is showing up. I think when I write to the very end of a file it's
zeroing out everything that comes before it. How do I avoid this
behavior? If I switch the ios to append, this doesn't happen.
However, I cannot garuntee that blocks will come in any kind of sorted
order.

I'm sorry I cannot elaborate more on all the varaibles listed above,
but this uses several classes, and I'm very confident that I'm not
runing into pointer problems.

Thanks for your help.

Nov 10 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
sleepylight wrote:
I'm running into a strange problem with ostreams's write function.
Please see the code below:

ofstream save((job->b->get_filename()).c_str(), ios::out |
ios::binary);
if (!save) {
cout << "Cannot open file" << endl;
return;
}
else {
if (job->b->get_offset() != 0)
save.seekp((job->b->get_offset() * BLOCK_SIZE)+1, ios::beg);
save.write(job->b->get_data(), (sizeof(char) *
job->b->get_size()));
cout << "Seek left the put pointer at byte " << save.tellp()
<<endl;
}
save.close()

This is just the snippit out of the middle of a file, but the idea is
pretty strait forward. I want to be able to make random reads and
writes into a binary file. If I write data to this file, close it,
open it, then seek past eof, it is my understanding that a write will
pad out any empty space with 0's.
Well sorry but your understanding is wrong.

Well, I'm writing a file in sequential blocks, and only the last block
is showing up. I think when I write to the very end of a file it's
zeroing out everything that comes before it. How do I avoid this
behavior? If I switch the ios to append, this doesn't happen.
However, I cannot garuntee that blocks will come in any kind of sorted
order.
I think what is happening is that you attempt to seek past then end of
the file are failing (as they should) and so you only ever write to the
beginning of the file.
I'm sorry I cannot elaborate more on all the varaibles listed above,
but this uses several classes, and I'm very confident that I'm not
runing into pointer problems.

Thanks for your help.


Solution is that if you need to move to a position past the end of a
file, then you must write out the intervening bytes yourself.

john
Nov 10 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.