我在使用ofstream将数字写入文件时遇到问题.当我写数字时,有像这样的字符而不是数字.我写入文件的方法是:
byte _b = 20;
ofstream p_file;
p_file.open("txt.txt", std::ios::app);
p_file << _b;
Run Code Online (Sandbox Code Playgroud)
有没有办法正确,或只是使用另一种文件编写方法?谢谢.
编辑:
p_file << (int) _b;
Run Code Online (Sandbox Code Playgroud)
工作良好.谢谢
我打赌那byte是char或其中的一些变体.在这种情况下,您将设置_b为具有代码20的字符,其中ASCII是控制字符.流输出将尝试输出字符而不是数字.
如果要获取数字,可以将其转换为另一种整数类型:
p_file << static_cast<int>(_b);
Run Code Online (Sandbox Code Playgroud)