(NEED HELP PLEASE)Writing data from a file into a matrix

when i write this array into the c++ debugger i just get a bunch of zero's set up in array form. the columns and rows are all right but i don't get the right values. What exactly do i have to do to get all the same integers?


yourfile >> FileRows ;
		int rows = atoi( FileRows.c_str() ) ;
		yourfile >> FileCols ;
		int cols = atoi( FileCols.c_str() ) ;
		yourfile >> Array ;
		cout << "The 2-dimensional Array you have created is" << rows << " rows by " << cols << " columns." << endl ; 

		for	( k = 0 ; k < rows ; k++ )
		{
			for ( l = 0 ; l < cols ; l++ )
			{
				
				matrix[k][l] = atoi (Array.c_str() ) ;
				cout <<  static_cast<int>(matrix[k][l]) << "\t" ;
			}
		  cout << endl ;
		}
		yourfile.close() ;
		 
	}
Last edited on
Input: 3 5 42
Output:
42	42	42	42	42	
42	42	42	42	42	
42	42	42	42	42	
Is that what you want?

1
2
3
4
yourfile >> FileRows ;
int rows = atoi( FileRows.c_str() ) ;
yourfile >> FileCols ;
int cols = atoi( FileCols.c_str() ) ;
Why don't just yourfile >> rows >> cols; ? they are numbers, so read them as numbers.
If you insist on doing the conversion use strtol or an stringstream and check for errors.
how do you use strtol? and also this is not supposed to be printed in "yourfile" rather in "cout" all those 42's must be all random numbers that were stored in ASCII in an output file.
But you aren't reading anything inside the loop.

man pages wrote:
The behaviour (of atoi(nptr)) is the same as strtol(nptr, (char **) NULL, 10); except that atoi() does not detect errors.
To check for errors in the conversion use errno and perror
Topic archived. No new replies allowed.