0

I'm trying to read in a file that contains matrix data into a boost matrix. "" is already supposed to have operator overloads for this sort of thing and I can get it to write to a standard stream (cout). I don't know what's wrong with going the other way. I'm fairly new to C++, so I'm guessing I'm making an incorrect assumption regarding file streams, but it seemed like it made sense. Here are the web pages I'm going on:

http://www.boost.org/doc/libs/1_51_0/boost/numeric/ublas/io.hpp

http://www.cplusplus.com/reference/iostream/ifstream/ifstream/

Here's my code:

using namespace std;
matrix<double> M;
ifstream s("C:\temp\perm.txt", ifstream::in);

s >> M;
s.close();

std::cout << M;

Here's what my file looks like:

[4,4]((0,0,1,0),(0,0,0,1),(0,1,0,0),(1,0,0,0))
beerncircus
  • 161
  • 1
  • 6
  • 2
    There is nothing wrong with what you have shown. [Here is a small example I made](http://ideone.com/0Kb8L), what is the problem? – Jesse Good Sep 16 '12 at 21:28
  • @Jesse But...something is strange on ideone. When I fork your program, I get the following error `prog.cpp:1:38: fatal error: boost/numeric/ublas/io.hpp: No such file or directory #include ` - here's the program - http://ideone.com/06Zsrf. What gives? – lifebalance May 11 '14 at 03:17
  • 1
    @lifebalance: ideone removed boost support. Use [coliru](http://coliru.stacked-crooked.com/a/bdb16549d800db84) instead as it supports boost. – Jesse Good May 11 '14 at 21:22
  • @JesseGood Thanks! - How do you specify the stdin on coliru? – lifebalance May 13 '14 at 16:03

1 Answers1

1

Here is a small example, please try it out and see what happens. If this doesn't work, I suspect that the problem is that the file path is wrong or the program is failing to read from the text file:

#include <boost/numeric/ublas/io.hpp>
#include <boost/numeric/ublas/matrix.hpp>
#include <iostream>
#include <fstream>

int main()
{
    boost::numeric::ublas::matrix<double> m;
    std::ifstream s("C:\temp\perm.txt");
    if (!s)
    {
        std::cout << "Failed to open file" << std::endl;
        return 1;
    }
    if (!s >> m)
    {
        std::cout << "Failed to write to matrix" << std::endl;
        return 1;
    }
    std::cout << "Printing matrix: ";
    std::cout << m;
}
Jesse Good
  • 50,901
  • 14
  • 124
  • 166