Reading numbers from file to array

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?)
Hello there!, i tried out your code, when debugging i can see that the array positions becomes true/false

i had to make my text file like this:

1
0
1
0
1

since it reads row by row.

EDIT: here is how i rewrote the code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
	std::ifstream Level1;
	Level1.open( "Level1.txt", std::ios_base::in );
	bool bLevel1[72];
	if (Level1.is_open())
	{
		
		int i = 0;
		while(!Level1.eof())
		{
			Level1 >> bLevel1[i];
			i++;
		}
		
		Level1.close();
	}


EDIT: The reason they all become true is because it reads 1010100101 like a single row.
Last edited on
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 


Hope this helps :)
Thanks but I don't really want to use strings.
I have to give you a huge thanks for all your help though, very inciteful, thanks!
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <fstream>

using namespace std;

int count = 0;
char bLevel1[72];

int main()
{
	std::ifstream Level1;
	Level1.open( "Level1.txt", std::ios_base::in );
	if (Level1.is_open())
	{
		
		while(!Level1.eof())
		{
			Level1.read( &bLevel1[count], 1 );
			if(bLevel1[count]!=10)  // Check for linefeed here and ignore it
				count++;
		}
		
		Level1.close();
	}
}
Last edited on
Topic archived. No new replies allowed.