Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

fstream problem 1

Status
Not open for further replies.

paskuda

Programmer
Jan 27, 2005
104
PL
Here's a little piece of code I'm trying to get to work:
Code:
char *bzium;
bzium = dupa.getHeader();
fstream xx("c:\\ble.dat",ios::binary|ios::out|ios::app|ios::trunc);
xx.write(bzium,strlen(bzium));
xx.close();

bzium is a non-empty null terminated string, c:\ble.dat exists and is not empty. What I'm trying to do is:
1. open ble.dat in write mode and truncate it
2. dump bzium into opened file
3. close it.
When I run this code it produces no errors, but also does not alter the file in any way. I googled, but all examples I found were similar.
Anyone sees the problem here ?

Mike
 
That works for me. Maybe there is a permissions problem with the file? Also, you don't need all those flags; ios::binary (if needed) and ios::eek:ut would be enough.
 
Actually, that's an Administrator account, there's no antivirus software, the file is not set read-only so I can't see any reason for problems with permissions. I'm pretty stuck here, so I'll probably have to do it the good old way, without fstream. Yet I'd like to know what's the reason of all that :/
 
Well, we try an experiment or two. Try this:
Code:
std::ofstream xx; // For the output file
xx.open("C:\\ble.dat", std::ios::app); // open for append and truncate
if (!xx)
{
    // there was an error opening stream up
    // process error here
}
else xx << bzium << endl; // I'm assuming bzium is a string
xx.close();

In the above error section, you can send an error message or trap the error conditon or both. Also, depending on how you've set up the standard namespace, std may not be necessary.

James P. Cottingham
-----------------------------------------
To determine how long it will take to write and debug a program, take your best estimate, multiply that by two, add one, and convert to the next higher units.
 
TonyGroves,
you were right about the flags, also it appears to me that ios::app was the reason of problem. After I removed it everything worked fine.
Anyway, another issue - I have a string like "\23\12\11\48" and would like to dump it to file so that first byte of the file has ascii code 23, second 12, etc - any ideas ? simple xx << bzium doesn't work well.
 
Do you mean that in the example, the string has 12 characters, and you want to output it as 4 bytes? Then, you could use something like:
[tt]
char bzium[]="\\23\\12\\11\\48";
char*p=0;
std::fstream f;
f.open("c:\\ble.dat",ios::binary|ios::eek:ut);
if(p=strtok(bzium,"\\"))
do
{
f<<(char)atoi(p);
p=strtok(0,"\\");
}
while(p);
f.close();
[/tt]
 
OK, I guess the example I gave was poor, I'll try to explain it better:
The string I have is a string of hexadecimal values separated with backslashes. Those values are between 0x0 and 0xff, they always take two bytes, as I'm padding them with zero in front when needed (like 0f, 0e, etc). As you can see, the values, when converted to decimal are 0 - 255.
So, the question: I have a sample string (length may vary) "\0e\ff\13\f1". I want to generate binary file containing of 4 bytes - first byte with ascii code 0x0e, second 0xff, third 0x13, fourth - 0xf1.
I just hoped that achieving this is just possible with formatting the string in some special way and just dumping it into fstream with << operator ...
 
Then you could just replace atoi(p) with a call to a function that converts a hex string to an integer; [tt]strtol(p,0,16)[/tt] for example.
 
TonyGroves
Actually, I found easy & effective way, which is not exactly what you posted before, but your suggestions were very helpful, thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top