• Advertisement

Archived

This topic is now archived and is closed to further replies.

Binary Saving

This topic is 5620 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Can someone give a basic example on saving in binary, then being able to read it back in? (Comments please!) Beatles are the best!

Share this post


Link to post
Share on other sites
Advertisement

  
#include <fstream.h>

ofstream file = ofstream("data.ccc", ios::binary);

int main(int argc, char *argv[])
{
int a = 14430;
int b = 0;

if(!file.is_open())
return 0;

file.write((char *) &a, sizeof(int));

cout << a << endl;

file.close();

cout << "Now we are going to read in your data\n";

ifstream fin = ifstream("data.ccc", ios::binary);

fin.read((char *) &b, sizeof(int));

cout << b << endl;

return 0;
}


Hope that helps!

Signatures are lame!! =)

Share this post


Link to post
Share on other sites
Since I avoid C++/STL streams like the plague, I use the following:


  
// this is old-school C code

FILE *file = fopen("filename.dat", "rb+");
// r = read (open file if exists), b = binary, + = allow read/write

// w = write (create file)


char buffer[200];
int i
fread(buffer, 1, 200, file);
fread(&i, 1, sizeof(int), file);

fseek(file, 0, SEEK_SET);
int file_position = ftell(file);

fclose(file);



and recently (in windows):


  
HANDLE file = CreateFile(/*A bunch of parameters here*/);
ReadFile(...);
WriteFile(...);
// tons of other functions

CloseHandle(file);


My preference on binary file stuff stems from the fact that some of the stream functions are improperly documented, causing major headaches. My only caution about above is when you read in structs, be VERY careful about byte/word/whatever size alignment. You never know whether C++ is using 4 bytes of memory to store your "unsigned char" in memory for efficiency reasons or not.

Share this post


Link to post
Share on other sites
I''m curious to know the proper way to store a floating point number in binary so that anything can read it. Is there anything special you need to do besides putting the bytes in the right order depending on if the machine is big endian or little endian, or is the way floating point numbers are stored pretty standard?

Share this post


Link to post
Share on other sites
quote:
Original post by smart_idiot
I''m curious to know the proper way to store a floating point number in binary so that anything can read it. Is there anything special you need to do besides putting the bytes in the right order depending on if the machine is big endian or little endian, or is the way floating point numbers are stored pretty standard?


Should be stored just like any other variable. Try it once and see. Best way to learn!!

Signatures are lame!! =)

Share this post


Link to post
Share on other sites
I know it will work on my computer, what I want to know is if I can do it on my computer, give it to someone with a completely different type of computer / os and still get the same result back.

Share this post


Link to post
Share on other sites
Yeeeaaahhh Nypyren! I tried using the stupid STL file thinger for my current project, but I got fed up with it, and used FILE instead. Works like a champ.

Share this post


Link to post
Share on other sites
quote:
Original post by MARS_999


    
#include <fstream.h>
#include <iostream.h>

ofstream file = ofstream("data.ccc", ios::binary);

int main(int argc, char *argv[])
{
file.setf(ios::showpoint | ios::fixed);
cout.setf(ios::showpoint | ios::fixed);
float a = 14430.019f;
float b = 0.0f;

if(!file.is_open())
return 0;

file.write((char *) &a, sizeof(float));

cout << a << endl;

file.close();

cout << "Now we are going to read in your data\n";

ifstream fin = ifstream("data.ccc", ios::binary);

fin.read((char *) &b, sizeof(float));

cout << b << endl;

return 0;
}



Signatures are lame!! =)

Share this post


Link to post
Share on other sites
quote:
Original post by smart_idiot
I know it will work on my computer, what I want to know is if I can do it on my computer, give it to someone with a completely different type of computer / os and still get the same result back.


Yes you can but you will have to code in endianess(byte swapping) e.g. PC(x86) to Mac(PPC) no dice unless you byte swap. I know I had a Mac and came back to my PC. (Waiting until IBM''s Power4 CPU is in the Macs) You need to check for the order of bits. High or low order and swap accordingly. Hope that helps.

Signatures are lame!! =)

Share this post


Link to post
Share on other sites

  • Advertisement