473,698 Members | 2,090 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1871
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
3398
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 server, it's running good the bat file copying files from target computer in the LAN to the local computer, o.k
10
3300
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 the backup is currently set up. When I did 'view contents' of DBBackup, I saw it contained the mixture of log, full and diff backups of verious dates. She has quit. Other coworkers said (in a not-sure tone) she was able to restore the databases...
12
2674
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 print a sorted report of html files and their size. (a copy of the script is here: http://xahlee.org/_scripts/check_file_size.pl )
17
2695
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 return to a program. so, excatly, I need to know the name of the executable file that is executed. originally, I try to windows service + FileSystemWatcher. but, There are just created, renamed, deleted, and changed four events in the...
9
3456
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 of this behavior is to prevent Xinclude instructions to point to "out of application directory" files when processed by the XSLT processor, among other things.
2
5256
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 in the text box of File control. But in the code behind when I try to get the FullFile path using file1.PostedFile.FilePath it is not giving the full path (c:\test\test.pdf). Instead it is giving only file name (test.pdf). If i try to do this on...
3
11189
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 Recovery model from FULL to Simple. Step2: go to task to manually shrink the log file Step3: Change recovery model back from simple to FULL. But by this way, I could get same problem again, the log file is fill,
4
1915
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 would like to achieve. Lets start with 2 pages, page1.aspx has the text boxes and page2 is where I want to display the informaiton. Page1.aspx
2
2044
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 files are sitting on a linux server. I know almost nothing about linux. The background: I was given a web share by my IT admin. Initially, the web share had 3 users, myself (as the owner) ,root (the group) and everyone. I could copy and paste from my...
65
5072
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 got errors of Failed to open file ./outdir/mytestout.txt. Below is the code: #include <stdio.h>
0
8598
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9016
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8887
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8856
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5858
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4613
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3037
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2321
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1997
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.