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))
4
  • 2
    There is nothing wrong with what you have shown. Here is a small example I made, what is the problem? Commented Sep 16, 2012 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 <boost/numeric/ublas/io.hpp> - here's the program - ideone.com/06Zsrf. What gives? Commented May 11, 2014 at 3:17
  • 1
    @lifebalance: ideone removed boost support. Use coliru instead as it supports boost. Commented May 11, 2014 at 21:22
  • @JesseGood Thanks! - How do you specify the stdin on coliru? Commented May 13, 2014 at 16:03

1 Answer 1

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

Comments

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.