I need someone to put me on the right track as I need to read a Notepad file that contains the following:

int mappy2_map0[4][4] = {
{ 0, 23, 23, 0 },
{ 0, 23, 23, 0 },
{ 0, 23, 23, 0 },
{ 0, 23, 23, 0 }

I have read a bit on streams, but I'm a bit baffled as this text file contains curly braces, commas, basically an actual coding for a 2d array and I don't understand how I can get the array elements from this text file and load them into a 2d array in my compiler/codes.

So if anyone can help then I will be really grateful. Maybe somehow check each character/row of values and see if they are strings or numbers,etc???


8 Years
Discussion Span
Last Post by K0ns3rv

If you know that the text file will be formatted EXACTLY as you show, then you can read and discard the first line (assuming the array size will not change). For the remaining lines, read and discard the first element, then read in numbers, discarding the commas. At its simplest, something like

char junk[100];
int temp_num;

fin >> junk;  //reads the opening { up to blank space
fin >> temp_num;
//store the num
fin >> junk; //reads the comma
fin >> temp_num
//store the num
//really, do this in a loop

Well basicly what you want to do is write it in the simplest form possible in the text file like so:

1 0 2 3
4 3 23 1
3 4 59 2

and then to get it you do the following

ifstream load("textfile.txt", ios::in);
int map[4][4];
for(int q=0;q <5 ; q++)
for(int x=0;x <5 ; x++){
load >> map[q][x];



Thanks guys, and i cant write it in its simplest form as this is an output from a map editor i'm using. But I will see if that is somehow possible!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.