I will have user input a file which will contain some data like this :
numRows numCols x x x ... x x x x ... x . .. ...
Now I am having trouble reading data from a file like this. I am not understanding what should I do to read each integer from each line. This is what I have so far:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class my_exception : public std::exception {
virtual const char *what() const throw() {
return "Exception occurred!!";
}
};
int main() {
cout << "Enter the directory of the file: " << endl;
string path;
cin >> path;
ifstream infile;
cout << "You entered: " << path << endl;
infile.open(path.c_str());
string x;
try
{
if (infile.fail()) {
throw my_exception();
}
string line;
while (!infile.eof())
{
getline(infile, line);
cout << line << endl;
}
}
catch (const exception& e)
{
cout << e.what() << endl;
}
system("pause");
return 0;
}
Also what I want is to store data at each line! That means after the first line I want to store data into the corresponding variable and each cell value.
I am confused as to how can I get each integer and store them in a unique(numRows and numCols) variable?
I want to save first two lines of the file into numRows
and numCols
respectively, then after each line's each integer will be a cell value of the matrix. Sample input :
2 2 1 2 3 4
TIA