Reading a Matrix txt file and storing as an array

前端 未结 5 1542
名媛妹妹
名媛妹妹 2020-12-03 15:34

I\'m currently writing a Simulated Annealing code to solve a traveling salesman problem and have run into difficulties with storing and using my read data from a txt file. E

5条回答
  •  孤城傲影
    2020-12-03 16:10

    Here is how I would load/save it:

    #include 
    #include 
    #include 
    
    int width = 0;
    int height = 0;
    double **distances;
    
    void WriteDouble( std::ofstream &stream, double toWrite )
    {
        char buffer[8];
        memcpy( buffer, &toWrite, 8 );
        stream.write( buffer, 8 );
    }
    
    void WriteInt( std::ofstream &stream, int toWrite )
    {
        char buffer[4];
        memcpy( buffer, &toWrite, 4 );
        stream.write( buffer, 4 );
    }
    
    double ReadDouble( std::ifstream &stream )
    {
        double d = 0;
        stream.read( (char *)&d, 8 );
        return d;
    }
    
    int ReadInt( std::ifstream &stream )
    {
        int i = 0;
        stream.read( (char *)&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 < width; x++ ) {
            for( int y = 0; y < 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 < width; i++ ) {
            distances[i] = new double[height];
        }
    
        for( int x = 0; x < width; x++ ) {
            for( int y = 0; y < height; y++ ) {
                distances[x][y] = ReadDouble( stream );
            }
        }
    
        stream.close();
    }
    
    void RunSaveTest()
    {
        width = 15;
        height = 15;
    
        distances = new double *[width];
    
        for( int i = 0; i < width; i++ ) {
            distances[i] = new double[height];
        }
    
        for( int x = 0; x < width; x++ ) {
            for( int y = 0; y < height; y++ ) {
                distances[x][y] = (double)x / (double)( y + 1 );
                std::cout << distances[x][y] << std::endl;
            }
        }
    
        Save();
    }
    
    void RunLoadTest()
    {
        Load();
    
        for( int x = 0; x < width; x++ ) {
            for( int y = 0; y < height; y++ ) {
                std::cout << distances[x][y] << std::endl;
            }
        }
    }
    
    int main()
    {
        RunSaveTest();
        // RunLoadTest();
    
        return 0;
    }
    

提交回复
热议问题