tags:

views:

106

answers:

3

i want to read a file which consists of 1's and 0's in each line, how do can i store a false in any bool variable after reading 0 from the file.

language c++

+2  A: 
bool b = (variableFromFile != 0);
Vlad
What about `variable != 0` to keep the nonzero-is-true convention?
AshleysBrain
@AshleysBrain: already this way
Vlad
Sneaky edit! :)
AshleysBrain
Actually, the edit was before your comment :-P
Vlad
Sneaky... time... page... latency... meh
AshleysBrain
+5  A: 

Just read into a boolean variable:

bool b;
file >> b;

where file is your input file stream. 1's will be converted to true, 0's to false.

If you have read into a string, the obvious way to do the conversion is to look at the first character:

string s = .... // populate
bool b = s[0] != '0';

but I can't see why you would read into a string if what you really want is a bool.

anon
i have made tokens of the line from file then.
shak
@shak Sorry, don't understand that.
anon
i am making sub strings of each line. so how to store from sting to bool , do i have to convert it to int first.
shak
A: 

read from file and assign to bool variable it should work.

sat2
Nice advices. And don't forget to turn on the computer as well.
ereOn