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();
2
  • Please std::cout word. Commented Apr 8, 2013 at 6: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. Commented Apr 8, 2013 at 6:26

1 Answer 1

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];
Sign up to request clarification or add additional context in comments.

4 Comments

This is a great solution, but I fear its to far away from the OPs to be useful.
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.
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.
@FreyaRen There is a large number of matrix libraries available. Some of them offer I/O capabilities. My personal favorite is eigen, but that doesn't have an easy read-matrix-from-file functionality. Another option is Boost.uBlas. This SO answer shows an example of reading a file with it.

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.