Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is how I would load/save it:</p> <pre><code>#include &lt;iostream&gt; #include &lt;fstream&gt; #include &lt;string&gt; int width = 0; int height = 0; double **distances; void WriteDouble( std::ofstream &amp;stream, double toWrite ) { char buffer[8]; memcpy( buffer, &amp;toWrite, 8 ); stream.write( buffer, 8 ); } void WriteInt( std::ofstream &amp;stream, int toWrite ) { char buffer[4]; memcpy( buffer, &amp;toWrite, 4 ); stream.write( buffer, 4 ); } double ReadDouble( std::ifstream &amp;stream ) { double d = 0; stream.read( (char *)&amp;d, 8 ); return d; } int ReadInt( std::ifstream &amp;stream ) { int i = 0; stream.read( (char *)&amp;i, 4 ); return i; } void Save() { std::ofstream stream( "cities", std::ios::out | std::ios::binary ); if( !stream.good() ) { throw std::exception( "Error opening stream" ); } WriteInt( stream, width ); WriteInt( stream, height ); for( int x = 0; x &lt; width; x++ ) { for( int y = 0; y &lt; height; y++ ) { WriteDouble( stream, distances[x][y] ); } } stream.close(); } void Load() { std::ifstream stream( "cities", std::ios::in | std::ios::binary ); if( !stream.good() ) { throw std::exception( "Error opening stream" ); } width = ReadInt( stream ); height = ReadInt( stream ); distances = new double *[width]; for( int i = 0; i &lt; width; i++ ) { distances[i] = new double[height]; } for( int x = 0; x &lt; width; x++ ) { for( int y = 0; y &lt; height; y++ ) { distances[x][y] = ReadDouble( stream ); } } stream.close(); } void RunSaveTest() { width = 15; height = 15; distances = new double *[width]; for( int i = 0; i &lt; width; i++ ) { distances[i] = new double[height]; } for( int x = 0; x &lt; width; x++ ) { for( int y = 0; y &lt; height; y++ ) { distances[x][y] = (double)x / (double)( y + 1 ); std::cout &lt;&lt; distances[x][y] &lt;&lt; std::endl; } } Save(); } void RunLoadTest() { Load(); for( int x = 0; x &lt; width; x++ ) { for( int y = 0; y &lt; height; y++ ) { std::cout &lt;&lt; distances[x][y] &lt;&lt; std::endl; } } } int main() { RunSaveTest(); // RunLoadTest(); return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload