473,408 Members | 2,477 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

File full of '^@' characters after binary write

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
1 1858
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Rocket Hawk | last post by:
Hello All, I am using windows 2000 server IIS 5.0 i was trying to run bat file from asp page and getting: Access Denied but when i run the bat file from the command prompt on the local...
10
by: Bing Du | last post by:
Greetings, Our former SQL Server 2000 DBA backed up everything in a single disk file. By everything I mean, full backup, differential backup and transaction logs. See below for details of how...
12
by: Xah Lee | last post by:
would anyone like to translate the following perl script to Python or Scheme (scsh)? the file takes a inpath, and report all html files in it above certain size. (counting inline images) also...
17
by: spentun | last post by:
How can I moitor file which is executed by user or program in window. I need to write a code to monitor file. When user clicks the file(.exe) or is called by program, the name of file will be...
9
by: Julien Biezemans | last post by:
Hi! Here is the problem: I'd like to restrict local filesystem stream operations to one directory just like a root jail. fopen('/file.bin') would actually open /some/path/file.bin. One goal...
2
by: Sridhar | last post by:
Hi, I have a web form where it has a <input type=file id=file1> control. I have an Upload button to upload the file. WHen I click on browse and select one file, it is showing the full file path...
3
by: danceli | last post by:
"The log file for database is full. Back up the transaction log for the database to free up some log space." Now I only know this way to deal with that manually, Step1. in option , chance...
4
by: CSINVA | last post by:
Trying to figure out how to do a cross post page within the code behind file. I need to do some calculations before I send a string value to another pages. Here is an example of the concept I...
2
by: beary | last post by:
Hello everyone, I posted this in unix/linux but it received no replies, so I assume it was the wrong forum. I'm trying here. I'm in way over my head with file permissions. The directory and...
65
by: Hongyu | last post by:
Dear all: I am trying to write to a file with full directory name and file name specified (./outdir/mytestout.txt where . is the current directory) in C programming language and under Unix, but...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.