I have looked for a few hours and couldn't find any info to fix my problem.
I'm having trouble with reading a text file with numbers and putting them into an boolean array.
Here's what I have
1 2 3 4 5 6 7 8 9 10 11 12
std::ifstream Level1;
Level1.open( "Level1.txt", std::ios_base::in );
bool bLevel1[72];
if (Level1.is_open())
{
for ( int i = 0 ; i < 72 ; i++ )
{
Level1 >> bLevel1[i];
i++;
}
Level1.close();
}
The text file look like this
101010101010
101010101010
101010101010
101010101010
101010101010
101010101010
What happens is all entries in bLevel1 become true.
If it isn't possible to do this then you can pretend the array is an int array.
Also, please let me know if a different formatting for the numbers in the file would make it possible to have more optimized code (not really important but doesn't hurt to know the ways of optimizing right?)
Thank soooooo much, I didn't know it read row by row, it works perfectly with the changed formatting in the text file. Thanks again.
I do have a little problem now though, that is if a users wants to change the look of the level, it would be quite difficult because the level is 12x6 which is why it looked like that in my first post.
So im wondering if it's possible to read a character from the file, put it into an array and repeat, rather than reading a line at a time
Also the i++ on line 9 is an error so that was a bit of a problem.
Hello again, you can try to read the whole line as a string and then check the characters in the string and apply them to the boolean array as true or false.
you can check the caracters in a string as a normal array
example:
1 2 3 4 5
std::string myString = "Hello";
std::cout<<myString[0]<<" "<<myString[1]<<std::endl;
// this will print H and e
Kree, you could try using the read function, I could only get this to work using chars instead of bools, but at least you can keep your grid shape in the text file. You may need to convert the ascii values (48 and 49) to 0 and 1 if you require that in your array.