c++ - how to write and read points to binary file? -
i've programmed code in c++ of ball moves in space (3d points). have movements positions. mean, path points passed.
i have write positions\points binary file , read in order restore movements\path. example, if move ball , right, i'll want save positions passed can read them , draw ball moves same, restore path.
i saw example binary file doesn't me:
// reading 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 in memory"; delete[] memblock; } else cout << "unable open file"; return 0; }
does create file automatically? where? , writing , reading points (x,y,z) ? should write through binary bytes? or points , file makes binary..?
you can write point (x,y,z) binary file separating coordinates e.g colons, , points semicolons:
int x=10, y=12, z=13; ofstream outfile("points.bin", ios::binary); if (!outfile) cerr << "could not open file" << endl; else outfile << x << ',' << y << ',' << z << ';';
Comments
Post a Comment