C++ Read File Into 2 Dimensional Array

Ok, my code seems to read the file but not from the very begging and adds some extra numbers at the end, also it does not display in an 8 by 8 array format?
Help?

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
#include <iostream>
#include <fstream>

using namespace std;

int main ()
{
	
int row, col;
int array [8] [8];

ifstream infile;

infile.open ("arraydata.txt");
if(!infile)
{
     cout << "File does not exist." << endl;
}

infile >> row >> col;

for(row = 0; row < 8; row++)
     for(col = 0; col < 8; col++ )
	{
	        infile >> array [row][col];
		cout << array [row][col] << ' ';
	}
	cout << '\n';	
infile.close();

return 0;
}


This is what it outputs.


45 92 50 88 32 75 84 34 23 0 99 42 50 108 45 21 12 9 72 93 55 100 109 4 8 29 60
18 10 2 13 43 47 72 5 38 62 70 85 22 21 0 19 46 80 201 5 210 102 59 172 33 25 10
409 40 28 9 70 118 120 20 4232367 4234352


This is my input file (arraydata.txt).


1 23 45 92 50 88 32 75
84 34 23 0 99 42 50 108
45 21 12 9 72 93 55 100
109 4 8 29 60 18 10 2
13 43 47 72 5 38 62 70
85 22 21 0 19 46 80 201
5 210 102 59 172 33 25 10
409 40 28 9 70 118 120 20


Can anyone help me please?
I feel your problem is that you're reading in two two numbers preemptively on line 20. I'm thinking what you want to do is store every bit of data from the file in the array.

tl;dr remove line 20
your program is almost done, compare what you missed:
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
#include <iostream>
#include <fstream>
using namespace std;

int main ()
{
	int array[8][8];
	PERSON *ptr;
	int lines;
	ifstream in("inf.txt");
	
	for(int row = 0; row < 8; row++)
     for(int col = 0; col < 8; col++ )
		{
	        in >> array [row][col];		
		}
		
	for(int row = 0; row < 8; row++)
	{
		for(int col = 0; col < 8; col++ )
		{
	       cout << array[row][col] << "\t";		
		}		
		cout << '\n';
	}	
	
return 0;
}
anup30 What is the PERSON *ptr;?
Last edited on
And thanks Keene removing line 20 displays all of the numbers begging to end without any extra numbers. but is there any way to display the output of the array to look like the input file dies?
You can. Your code is almost correct in doing that, but you didn't put brackets around your first for-loop.

1
2
3
4
5
6
7
8
9
for(row = 0; row < 8; row++)
    {
     for(col = 0; col < 8; col++ )
	{
	        infile >> array [row][col];
		cout << array [row][col] << ' ';
	}
	cout << '\n';
    }


This will output a newline every row.
that was a typo, remove it.
is there any way to display the output of the array to look like the input file dies?

line18 to 25 of my previous code.
Oh ok i missed that part. lol. alright thank you guys a lot.

I just have one more question.

is there any way to name the rows and columns in the output?
show what output format you want to see

col 1 col 2 col 3 col 4 col 5 col 6 col 7 col 8
Row 1 1 23 45 92 50 88 32 75
Row 2 84 34 23 0 99 42 50 108
Row 3 45 21 12 9 72 93 55 100
Row 4 109 4 8 29 60 18 10 2
Row 5 13 43 47 72 5 38 62 70
Row 6 85 22 21 0 19 46 80 201
Row 7 5 210 102 59 172 33 25 10
Row 8 409 40 28 9 70 118 120 20


Something like this...
Last edited on
use 2 loops. first one for 1st row of cols and 2nd one for the rest. use '\t'
ok this is making my head hurt. what would the loops look like? and does it go before or after the file closes?
before or after the file closes?
it does not matter, cause u r saving them in array.

u can use <iomanip> for formatting

1 loop for:
col 1 col 2 col 3 col 4 col 5 col 6 col 7 col 8

another for:
Row 1 1 23 45 92 50 88 32 75
Row 2 84 34 23 0 99 42 50 108
Row 3 45 21 12 9 72 93 55 100
Row 4 109 4 8 29 60 18 10 2
Row 5 13 43 47 72 5 38 62 70
Row 6 85 22 21 0 19 46 80 201
Row 7 5 210 102 59 172 33 25 10
Row 8 409 40 28 9 70 118 120 20

the latter loop is almost shown in line 18 to 25 in my previous code.
just add cout << "Row " << row+1 << " ";
Topic archived. No new replies allowed.