Reading a file and storing the data into 2D array

closed account (yT72wA7f)
Im trying to get my code to read in a file with 10 lines and 10 characters on each line and then store each character read in into a 10X10 2D character array. The characters will be separated by spaces and each new line represents a new row in the 2D array.

My problem is that it doesn't store the data into the 2d array unless I use the cout in the second double loop.


File2.txt
a b c d e f g h i j
a b c d e f g h i j
a b c d e f g h i j
a b c d e f g h i j
a b c d e f g h i j
a b c d e f g h i j
a b c d e f g h i j
a b c d e f g h i j
a b c d e f g h i j
a b c d e f g h i j


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include<iostream>
#include<fstream>
#include <string>
#include <stdio.h>

using namespace std;
int main() {

	const int ROWS = 10;
	const int COLS = 10;
	char array[ROWS][COLS];

	ifstream inputFile;
	inputFile.open("File2.txt");

	for (int i = 0; i < ROWS; ++i) {
		for (int j = 0; j < COLS; ++j) {
			inputFile >> array[i][j];
		}
	}
	inputFile.close();


	for (int i = 0; i < ROWS; ++i) {
		for (int j = 0; j < COLS; ++j) {
			cout << array[i][j] << " ";
		}
		cout << endl;
	} 

	return 0;
}

Last edited on
First of all, your file has 10 rows of 10 characters. That's OK because your array is actually 10x10.

The second double loop only prints out what was already stored into the 2D array. Your array is populated in lines 16 - 20. Nothing is printed out to tell you this, but that is where the array is populated.

When you add line 24 - 29, you merely print out what was stored in the 2D array, confirming that you stored the data properly.
closed account (yT72wA7f)
But then why can't I print out the array outside of the loops if the data is properly stored inside? If I can't print out the array outside of the loops then wouldn't that me that the file data wasn't stored in the 2D array?
Last edited on
This same guy (with a different name) was asking these same bizarre trolling questions yesterday.
closed account (yT72wA7f)
Just because someone has a similar question about arrays and files compared to another user doesn't make then the same person.
JamesTheMan wrote:
But then why can't I print out the array outside of the loops if the data is properly stored inside?


Could you show us the code that you were trying to use to "print the array outside of the loops."


Also:
Why are including <stdio.h> BTW? Please tell us why you have included it. What do you think it does?
Last edited on
closed account (yT72wA7f)
I was just trying to cout << array[i][j] outside of the loops to see if the data was stored in them, because shouldn't I be able to access and print the array outside of the loops? And <stdio.h> is the library for input and output operations? At least that's what I thought it was for, but correct me if I'm wrong.
JamesTheMan wrote:
I was just trying to cout << array[i][j] outside of the loops

I'm asking to see the code you used for that.

There is no conceivable way to answer your question without it.



JamesTheMan wrote:
And <stdio.h> is the library for input and output operations?

For C-style output; e.g. with printf. But that's not what you are using. For streamed io (which is what you are using) you just need <iostream>.
Last edited on
I agree that we need to see the code that you tried that did not work. Without it, we are only guessing what your problem is.

Speaking of guessing, I am assuming that you tried to add cout << array[i][j]; somewher around line 22. If that is the case, i and j hold values that caused the program to break out of the loops (i == ROWS, j == COLS). In this case, array[i][j] returns a char that is outside of the array because the last element is array[ROWS-1][COLS-1].

If you really don't want to use a second set of nested loops, you can use a debugger to see what is stored in memory as you step through your code. Talk to your teacher about what debuggers you have available and how to use them.

Otherwise, the only ways* to print out the contents of your array is to use a nested loop (as you did in lines 24 - 29 above) or to manually unroll it yourself, such as:

1
2
3
4
cout << array[0][0] << " " << array[0][1] << " " << ... << array[0][9] << endl;
cout << array[1][0] << " " << array[1][1] << ...
...
cout << array[9][0] << " " << array[9][1] << ...


* You can also use a single loop and calculate the correct row and column from the loop index, but that's a little bit more complex when you already have a 2-dimensional array.
Topic archived. No new replies allowed.