0

I've programmed a code in C++ of a ball that moves in the space (3D points). I have all its movements positions. I mean, all the path points it passed.

I have to write its all positions\points into a binary file and then read it in order to restore the movements\path. for example, if I move the ball up and right, I'll want to save all the positions it passed so then I can read them and draw the ball moves the same, restore its path.

I saw an example for binary file but it doesn't say much to me:

 // reading a complete binary file
 #include <iostream>
 #include <fstream>
 using namespace std;

 ifstream::pos_type size;
 char * memblock;

 int main () {
   ifstream file ("example.bin", ios::in|ios::binary|ios::ate);
   if (file.is_open())
   {
     size = file.tellg();
     memblock = new char [size];
     file.seekg (0, ios::beg);
     file.read (memblock, size);
     file.close();

     cout << "the complete file content is in memory";

     delete[] memblock;
   }
   else cout << "Unable to open file";
   return 0;
 }

Does it create the file automatically? Then where? And what about writing and reading points (X,Y,Z) ? Should I write it through binary bytes? or as points and the file makes it binary..?

5
  • Why do you want to read and write into binary file? Why not text format? Commented Jul 25, 2013 at 11:54
  • Your example just reads a file into memory with no interpretation of what the file actually contains and no example of writing the file. Keep Googling - there are plenty of examples out there... Commented Jul 25, 2013 at 11:56
  • herolover, I want to implement it in a function that is called each frame(Update function) so I don't need a big file but small. and John3136, I keep googling (: Commented Jul 25, 2013 at 12:00
  • But text format is human readable and better for debug. Are your points floating? If points are integer then memory overhead is not large. Commented Jul 25, 2013 at 12:07
  • the points are floats Commented Jul 25, 2013 at 12:08

1 Answer 1

1

You can write a point (X,Y,Z) to a binary file separating coordinates e.g by colons, and points by semicolons:

int X=10, Y=12, Z=13;
ofstream outfile("points.bin", ios::binary);
if (!outfile)
    cerr << "Could not open a file" << endl;
else
    outfile << X << ','
            << Y << ','
            << Z << ';';
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.