Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualEddieV223

Posted 24 March 2013 - 05:26 PM

You can save in text or binary files.  Binary is much faster.  If you use fstream's then you specify ios::binary in the ctor. 

 

You need to know the format of the binary file when you read it back in.  Often files will have a header part and then the data part.  The header gives information about what to expect in the data part.

 

http://www.cs.fredonia.edu/~arnavut/classes/cs221/binaryfiles.pdf

 

When writing to a binary file stream use the .write() method, when reading use the .read() method.

 

// these two lines write an int

int myInt = GetSomeInt();

ifs.write( (char*)&myInt, sizeof(myInt) );

 

// these two lines read an int

int myInt = 0;

ifs.read( (char*)&myInt, sizeof(myInt) );

 

In both cases you take your int get its address via the address of operator and cast it to char*, since a char should be 1 byte.  The next parameter is the amount of bytes to read or write, since the example uses an int we use sizeof(myInt) which on most systems should be 4.  So these two functions take a starting address and a count of how many bytes to read/write.


#1EddieV223

Posted 24 March 2013 - 05:20 PM

You can save in text or binary files.  Binary is much faster.  If you use fstream's then you specify ios::binary in the ctor. 

 

You need to know the format of the binary file when you read it back in.  Often files will have a header part and then the data part.  The header gives information about what to expect in the data part.

 

http://www.cs.fredonia.edu/~arnavut/classes/cs221/binaryfiles.pdf


PARTNERS