I'm trying to build a matrix in C++ by reading the a file called matrix.txt and storing their values so it's easier for me to run my other algorithm. I'm pretty new to using fstream, so this is probably not the most efficient code, but this is my attempt so far (note, most of the classes are for other algoritms).
I decided to test so I could see if it works fine. But I seem to be bumping into this problem when I get to the end of a line. The text file values are separated by a delimiter ',' but it's rows are separated by line breaks. When I execute this code (with comments at the bottom) I get 2 values. The last value on the line and the first of the next line. But when I remove the comments I don't get that problem. Is there a way to fix this?
My code:
#include <iostream>
#include <list>
#include <math.h>
#include <iomanip>
#include <algorithm>
#include <fstream>
#include <string>
using namespace std;
int main(){
string str[80][80];
ifstream eulereightyone;
int a = 0;
int b = 0;
eulereightyone.open("matrix.txt");
if (!eulereightyone)
{
cout << "couldn't open file" << endl;
system("pause");
return 0;
}
while (eulereightyone.good())
{
getline(eulereightyone, str[a][b], ',');
if (a > 78)
{
a = 0;
++b;
}
a++;
}
//cout << str[78][1] << endl;
cout << str[79][1] << endl;
//cout << str[0][2] << endl;
system("pause");
return 0;
}