c++ - leading 0's are not getting read from file -
this question has answer here:
i reading content of file character character:
the file has:
000001111100000
when use code read
int content;// converting char result in proper output. std::ifstream fin; fin.open("zero_one"); fin>>content; while (!fin.eof()) { std::cout<<content; fin>>content; } fin.close();
i output as: 1111100000
(the leading zeros not present) trailing zeros present.
the concern is: using >>
operator read then, why don't leading zeros in output whereas trailing zeros in output.
also, if convert int
char
content
variable output same content of file. why so? far know, difference between char
, int
size in bytes of variable.
if content
int
, reads data single integer: int
value 1111100000. if content
char
, reads each character separately.
you can see difference if, in while
loop, std::cout<<content<<'\n';
instead of std::cout<<content;
. if content
int
see 1 line containing 1111100000, if it’s char
see 15 lines, 1 each character of input.
if put spaces between each character of input file, read separate int
s , results same, there still important difference: values of char
s aren’t char(0)
, char(1)
, '0'
, '1'
(0x30
, 0x31
, respectively), ascii values of corresponding digits. int
s on other hand 0
or 1
.
Comments
Post a Comment