Reading a matrix into a file, getting ascii values!?

Hi im writing a program for my c++ class and im having trouble reading a file and inputing into a twodimensional array. When i run the program, it gives me its some numbers, im thinking its the ascii values for each number

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
35
36
37
38
39
40
41
42
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>
using namespace std;

int main()
{
	ifstream infile;
	infile.open("input.dat");
	if (infile.fail())
	{
		cout << "File could not be opened" << endl;
		exit(1);
	}
	int matrix[3][3];
	infile >> matrix[3][3];
	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			matrix[i][j];
		}



	}
	
	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			cout << setw (3) << matrix[i][j];
		}



	}
	

	return 0;
}

the output is -858993460 repeated 9 times
The file was
1 2 3
4 5 6
7 8 9
If anybody can help me figure this out it would be much appreciated
Last edited on
It probably has to do with the fact the file contains ASCII characters so you can read it in text format. These characters are different from actual integer values in coding. Just as an example the byte that contains an '1' character looks like this 0011 0001 as apposed to a byte containing a integer value of 1
looks like this 0000 0001. You cannot perform math on ASCII characters(at least when not expecting a proper result).

When you input the value into the integer matrix it is not changing the value but just storing the same value in a memory location of size int.
Last edited on
answers for char "digit" character conversion :

http://stackoverflow.com/questions/5029840/convert-char-to-int-in-c-and-c

answers for string of chars conversion :

http://stackoverflow.com/questions/1321137/convert-string-containing-several-numbers-into-integers

I'm sure you could possibly find a function or other answers if these do not satisfy your requirements.
Last edited on
Topic archived. No new replies allowed.