I'm doing a program that I need t read from a file some information.
That information consist in a level number and a board information like this: level-1
000000
000000
001100
001100
000000
000000
level-2
000000
000000
011110
011110
000000
000000
My question is: How can I read a "level" independent of the other information on the file?
I already try with some code on th forum, but didn't solve the problem.
Yep, your code will deal with "0 0 1 1 0 0" but not "001100" (i.e. space between the numbers?)
Edit:
If you mean space between the numbers? i.e. "0 0 1 1 0 0" versus "001100", then...
If you're using an int array for your board, and reading into it directly, then yes; you'll need spaces.
If it uses chars, no.
But if you do want to use an int array for some reason, then you could read into a temporary char variable and then use that to set the int array element. This will also handle the no-space case.
(Or you could even read e.g. "001100" into a string and then extract the chars one by one...)
Andy
PS You can do with out the close() at the end; ifstream's destructor will call it for you.
I suggest storing all the information in a string using getline. If this file is huge then have a special delineating character. Use '\0' if you want the whole file. Then use string::find to find where "level-1" is. Use string::find to find where "level-2" is. Use string::substr to save everything inbetween to another string. After that you would go one at a time and search every element and save it to a vector.