I have an input file that looks like this:
3, 2, 5
2, 4, 9
6, 5, 9
And I've made an array[3][3]
.
My question is: how can I read this into my array, while skipping the spaces and commas?
Also: I can't use stoi
(don't have C++11) and I haven't covered vectors yet.
I've tried the code block below, but my resulting array is full of zeroes.
string sNum; // Current number (as string)
int num = 0; // Current number (as int)
ifstream inFile; // Make input file stream
inFile.open("Numbers.txt"); // Open my input file of 3x3 numbers
while (getline(inFile, sNum, ',')) { // Gets 'line' from infile ..
// puts it in sNum ..
// delimiter is comma (the problem?).
for (int rowCount=0; rowCount<3; rowCount++) { // Go through the rows
for (int columnCount=0; columnCount<3; columnCount++) { // Go through the columns
num = atoi(sNum.c_str()); // String 'sNum' to int 'num'
array[rowCount][columnCount] = num; // Put 'num' in array
} // end columnCount for
} // end rowCount for
} // end while
I think it is reading in the spaces. How do I ignore the spaces in between getting my ints?