File full of '^@' characters after binary write

S

sleepylight

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::eek:ut |
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.
 
J

John Harrison

sleepylight said:
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::eek:ut |
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
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top