0

I save a boolean matrix(ROW*ROW) to .txt file (0,1 format).

How could I read the matrix from the file? I wrote code below, but I compare the read results with the file, and the array results don't match the file. Could anyone tell me where I wrote wrong? Or are there any easier ways to read a matrix file?

bool **results = new bool*[ROW];
    for(int i=0;i<ROW;i++){
        results[i] = new bool[ROW];
    }

    ifstream infile;
    infile.open ("FRM.txt");
    string line;
    int row=0;
    if (infile.is_open())
    {
        while ( infile.good() )
        {
            getline(infile,line);
            istringstream iss(line);
            int col=0;
            while(iss)
            {
                string word;
                iss >> word;
                int number = atoi(word.c_str());
                if(number==1){
                    results[row][col] = true;
                }
                else{
                    results[row][col] = false;
                }
                col++;
            } 
            row++;
        }
    }
    infile.close();
Freya Ren
  • 2,086
  • 6
  • 29
  • 39
  • Please std::cout word. – Alex Chamberlain Apr 08 '13 at 06:12
  • I know where the problem is. When I saved the matrix, there is a whitespace after each number. So when I read a line, I got something like "0 1 1 1 ", the last whitespace caused the error. Because "string word" would actually process the "", and deem it as integer 0. – Freya Ren Apr 08 '13 at 06:26

1 Answers1

1

Representing a matrix as a nested array is usually a bad idea. Use a linear array instead and do the index-juggling through a macro or inline function.

I would go for something like this:

#include <algorithm>
#include <cmath>
#include <cstdint> // uint8_t requires C++11
#include <iterator>
#include <vector>

// don't use vector<bool>, it creates all sorts of problems
std::vector<uint8_t> results;
{
  std::ifstream infile('FRM.txt', std::ifstream::in);
  if(infile.is_open() && infile.good())
  {
    std::istream_iterator<uint8_t> first(infile), last;
    copy(first, last, std::back_inserter(results));
  }
}

// only if you know the matrix is square! error checking strongly advised!
size_t nrows = size_t(std::sqrt(results.size()));

// accessing element (i,j) is then as easy as
size_t i = 2, j = 3; // assuming nrows > i,j
bool a_ij = results[i*rows+j];
Michael Wild
  • 24,977
  • 3
  • 43
  • 43
  • This is a great solution, but I fear its to far away from the OPs to be useful. – Alex Chamberlain Apr 08 '13 at 06:21
  • 1
    I believe that one should not simply offer the easiest fix for a broken code... The code by the OP clearly shows that C++ is not his/her strongest point, and hence can profit from a better example. – Michael Wild Apr 08 '13 at 06:26
  • When I use matlab, I could simply save the matrix to .mat file. So I was wondering, is there a better way for C++ to save matrix, and read matrix. – Freya Ren Apr 08 '13 at 06:28
  • 1
    @FreyaRen There is a large number of matrix libraries available. Some of them offer I/O capabilities. My personal favorite is [eigen](http://eigen.tuxfamily.org), but that doesn't have an easy *read-matrix-from-file* functionality. Another option is [Boost.uBlas](http://boost.org/libs/numeric). This [SO answer](http://stackoverflow.com/a/12450950/159834) shows an example of reading a file with it. – Michael Wild Apr 08 '13 at 06:31