hey i have a int **grid; pointer which should read from a txt file a grid which is made up for 0's and 1's, how would i make my grid pointer only read in 1 int for each index of grid[0]?

txt file contains height and weight of the grid then, the info in the grid.
4 4
1111
1100
0010
0101

Recommended Answers

All 4 Replies

read them in as characters so that you can read them one character at a time.

is there any other way like to use setprecision to only read in 1 thing?

setprecision() is for setting the decimal output of a stream.

If your grid is just characters, you should just use chars, then:

If you have the file already opened, and your pointer allocated, then just use fstream.read().

read() may not work because it would also read in the '\n' line terminators. I think call get() to read each character one at a time would work, convert the char to int when inserting into the grid

char c;
ifstream in("filename.txt");

in.get(&c);
// or this
in >> c;

grid[i][j] = c-'0'; // convert char to int
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.