1

I am trying to open a .mtx file and print the data out, mainly to see how the data is loaded. I want to use the data itself as soon I can load it all, but currently, it loads the first line only, and says it fails after that line.

Debug Assertion Failed! Program:..etc1 - Opening MTX FILE\Debug\Project1 - Opening MTS FILE.exe File:...(something) Line:3204 Expression: string subscript out of range

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
    long int n = 0;
    string line;
    ifstream data("/Users/MYPC/Desktop/matrix.mtx");
    if (data.is_open() == true)
        cout << "File opened successfully" << endl;
    while (n != sizeof(line))
    {
        getline(data, line);
        while (n!=sizeof(data))
        {
        cout << line[n];
            n++;
        }
    }
    system("pause");
    return 0;
}
  • What error do you get? – frido Mar 05 '19 at 10:28
  • Debug Assertion Failed! Program:..etc1 - Opening MTX FILE\Debug\Project1 - Opening MTS FILE.exe File:...(something) Line:3204 Expression: string subscript out of range –  Mar 05 '19 at 10:35
  • Thanks, you might want to add tags with more watchers to your question to get more views, e.g. `c++`. And is the tag `genfromtxt` realy applicable? – frido Mar 05 '19 at 10:53
  • Added C++.Dhowever, i believe the "txt" might be missleading as I am trying to get data from a .mtx file and not .txt. –  Mar 05 '19 at 10:57
  • It can open the file without a problem, it seems. But reading the data within it it fails. Dont know why. –  Mar 05 '19 at 10:59
  • `sizeof` is not the right operator to use to get a string length. You meant `while (n != data.length())`. The outer while loop is also useless, see [this answer](https://stackoverflow.com/a/7868986/1548468) instead. – Botje Mar 05 '19 at 12:04
  • Thank you Botje. I removed getline() completely and it reads the file properly. –  Mar 05 '19 at 12:27

0 Answers0