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 Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Writing binary data

Status
Not open for further replies.
Joined
Jan 20, 2005
Messages
65
Location
GB
I need to write data to a binary file.

Integers and strings I'm fine with. But how can I write a float as binary data?
 
Code:
float bouy;
FILE* xxx;
...
fwrite (&bouy, 1, sizeof (bouy), xxx);
 
Due to the nature of the program, I'm writing this to a buffer first and then using WriteFile, so I can't use this method. I'm using VS 2005.
 
Copy it to your buffer using memcpy() then.


--
 
I currently have a UCHAR array that I'm 'sprintf'ing too. I'm then writing the array to a newly created file!
 
Then it's a text file, not a binary file.

I think this is what the previous poster meant:
Code:
float bouy ;
UCHAR ocean[bignumber] ;

memcpy( &bouy, ocean + amt, sizeof( bouy ) ) ;
amt += sizeof( bouy ) ;
 
The file I am writing to as a binary file, not a text file.
 
You could actually post some real code rather than just posting one-line counter arguments to everything which is being suggested.


--
 
Ok, let's talk definitions...

A binary file contains the raw data. So if you're writing long integers containing 1,2,3,4,5, your sequence of bytes will be
Code:
0x1,0x0,0x0,0x0,0x2,0x0,0x0,0x0,0x3,0x0,0x0,0x0,0x4,0x0,0x0,0x0,0x5,0x0,0x0,0x0
Is this what you want?

Or do you want "1,2,3,4,5"? That's called either a Text file or an ASCII file, not binary.

 
I'm sorry there has been some confusion here - I am not not writing to a text file, I am writing integers as binary data.

Code:
float fVal;
UCHAR uFileImage[964];

memcpy(&fVal, uFileImage + fOffset, sizeof(float));

This code fragment solves my problem. Thanks everyone.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top