Read char from txt file in C++

Posted by Jack in the Box on Stack Overflow See other posts from Stack Overflow or by Jack in the Box
Published on 2012-09-07T15:19:44Z Indexed on 2012/09/07 15:38 UTC
Read the original article Hit count: 166

Filed under:
|
|
|

I have a program that will read the number of rows and columns from a txt file. Also, the program has to read the contents of a 2D array from the same file.

Here is the txt file

8 20
 *       
  *
*** 


         ***

8 and 20 are the number of rows and columns respectively. The spaces and asterisks are the contents of the array, Array[8][20] For example, Array[0][1] = '*'

I did make the program reading 8 and 20 as follow:

ifstream myFile;
myFile.open("life.txt");

if(!myFile) {
    cout << endl << "Failed to open file";
    return 1;
}

myFile >> rows >> cols;
myFile.close();

grid = new char*[rows];
for (int i = 0; i < rows; i++) {
    grid[i] = new char[cols];
}

Now, how to assign the spaces and the asterisks to to the fields in the array?

I hope you got the point.

© Stack Overflow or respective owner

Related posts about c++

Related posts about arrays