1

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

2
  • 1
    As far as I know, you are just reading each line out of the file and printing it out. If you want to store the values in an array of sorts then you've to do more than just reading out. One way would be to read out the first two lines, store it, then loop to read each line and then loop to read each character from that line. Commented Oct 12, 2016 at 6:24
  • @ctrl-shift-esc: How do I do that exactly ? :D Commented Oct 12, 2016 at 6:43

1 Answer 1

1

Try this out. The first line reads in the path. Then, using freopen we associate the file provided with stdin. So now we can directly use cin operations as if we are directly reading from stdin and as if the input from the file is typed line for line into the console.

Following this, I create two variables corresponding to numRows and numCols and create a matrix of this dimension. Then I create a nested for loop to read in each value of the matrix.

string path;
cin >> path;
freopen(path.c_str(),"r",stdin);

int numRows, numCols;
cin >> numRows >> numCols;

int matrix[numRows][numCols];   

for(int i = 0; i < numRows; i++){
    for(int j = 0; j < numCols; j++){
        cin >> matrix[i][j];
    }
}

Alternatively you can use this to create your matrix

int** matrix = new int*[numRows];
for(int i = 0; i < numRows; i++)
    matrix[i] = new int[numCols];

See this for more reference.

Sign up to request clarification or add additional context in comments.

7 Comments

If this works, it would be amazing!! Let me try that! :)
I get an error at this line int matrix[numRows][numCols] saying _expression must have a constant value_ ?
I understood the error. It means I have to initialize the array to some value. But how will I know size of the array from before ?
which IDE are you using ?! I use VS2015 and I read somewhere that it will not compile in the visual studio compiler
I tried the other alternative to create a dynamic array, but it says identifier numRows is undefined. Same error for numCols.
|

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.