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: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.
Please see the code below:
ofstream save((job->b->get_filename()).c_str(), ios: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.