Reputation: 199
I have a problem with writing number to a file with ofstream. When i write numbers there are characters like this █ instead of numbers. The method i write to the file is:
byte _b = 20;
ofstream p_file;
p_file.open("txt.txt", std::ios::app);
p_file << _b;
Is there any way to be right, or just use another filewriter method? Thanks.
EDIT:
p_file << (int) _b;
works fine. Thanks
Upvotes: 0
Views: 612
Reputation: 234384
I bet that byte
is char
or some variant thereof. In that case you are setting _b
to the character with code 20, which in ASCII is a control character. The stream output will try to output the character not the number.
You can cast it to another integral type if you want to obtain the number:
p_file << static_cast<int>(_b);
Upvotes: 6
Reputation: 206518
Change
byte _b = 20;
to
int _b = 20;
byte
is probably typedef to char
somewhere in your code.
Upvotes: 0
Reputation: 361322
What is byte
in your code? I assume it is a typedef of unsigned char
. Note C++ doesn't have byte
as data-type.
If so, then p_file
prints a character whose ASCII value is 20
. That is what you see in the file.
Do this if you want it to print 20
instead :
p_file << (int)_b;
Or, simply change the data type of _b
from byte
to int
.
Upvotes: 2