为什么 std::hex 和 std::oct 标志不起作用?
Why are std::hex and std::oct flags not working?
这是我的代码:
// This program demonstrates the use of flags.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string filename; bool tf; double number;
cout << "Name a file to create/overwrite: ";
cin >> filename;
ofstream outfile (filename.c_str());
if(outfile.fail())
{
cout << "Creating/Overwriting the file has failed.\nExiting...\n";
return 1;
}
cout << "Give me a boolean (0/1): "; cin >> tf;
cout << "Give me a large number with decimal points: "; cin >> number;
outfile.setf(ios_base::boolalpha); // Turns on boolalpha flag.
outfile << "Here's a boolean: " << tf << endl;
outfile.unsetf(ios_base::boolalpha); // Unsets boolalpha flag.
outfile << "Here's your number: " << number << endl;
outfile.setf(ios_base::scientific); // Turns on scientific notation flag.
outfile << "Here's your number is scientific notation: " << number << endl;
outfile.setf(ios_base::fixed); // When possible, floating point numbers will not appear in scientific notation.
outfile << "Here's your number in fixed notation: " << number << endl;
outfile.setf(ios_base::hex); // Numbers will appear in hexadecimal format.
outfile << "Here's your number in hexadecimal format: " << number << endl;
outfile.setf(ios_base::oct, ios_base::uppercase); // Numbers will appear in uppercase, octal format.
outfile << "Here's your number in octal format: " << number << endl;
return 0;
}
当我运行这个...
test.txt的内容:
Here's a boolean: false
Here's your number: 3491.67
Here's your number is scientific notation: 3.491670e+03
Here's your number in fixed notation: 3491.67
Here's your number in hexadecimal format: 3491.67
Here's your number in octal format: 3491.67
为什么当我设置 "hex" 和 "oct" 标志时,它们不起作用?
在文本文件中,我期待 "hexadecimal formal: " 和 "octal format: " 旁边的“3591.67”以外的内容。
我是否实施了错误的标志?
不幸的是,八进制和十六进制打印仅适用于整数,而不适用于双精度。参见 http://stdcxx.apache.org/doc/stdlibug/28-3.html
如果你想使用setf,应该是:
outfile.setf(ios_base::hex,ios_base::basefield);
。或者,输入 std:hex,即:
outfile << std::hex;
.
八进制和十六进制格式仅影响整数的显示方式。如果您想查看十六进制浮点数,您可以使用 hexfloat
(C++11) 或使用 cstdio
中的 printf
函数和 %a
格式代码。
另见 Dump hex float in C++
这是我的代码:
// This program demonstrates the use of flags.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string filename; bool tf; double number;
cout << "Name a file to create/overwrite: ";
cin >> filename;
ofstream outfile (filename.c_str());
if(outfile.fail())
{
cout << "Creating/Overwriting the file has failed.\nExiting...\n";
return 1;
}
cout << "Give me a boolean (0/1): "; cin >> tf;
cout << "Give me a large number with decimal points: "; cin >> number;
outfile.setf(ios_base::boolalpha); // Turns on boolalpha flag.
outfile << "Here's a boolean: " << tf << endl;
outfile.unsetf(ios_base::boolalpha); // Unsets boolalpha flag.
outfile << "Here's your number: " << number << endl;
outfile.setf(ios_base::scientific); // Turns on scientific notation flag.
outfile << "Here's your number is scientific notation: " << number << endl;
outfile.setf(ios_base::fixed); // When possible, floating point numbers will not appear in scientific notation.
outfile << "Here's your number in fixed notation: " << number << endl;
outfile.setf(ios_base::hex); // Numbers will appear in hexadecimal format.
outfile << "Here's your number in hexadecimal format: " << number << endl;
outfile.setf(ios_base::oct, ios_base::uppercase); // Numbers will appear in uppercase, octal format.
outfile << "Here's your number in octal format: " << number << endl;
return 0;
}
当我运行这个...
test.txt的内容:
Here's a boolean: false
Here's your number: 3491.67
Here's your number is scientific notation: 3.491670e+03
Here's your number in fixed notation: 3491.67
Here's your number in hexadecimal format: 3491.67
Here's your number in octal format: 3491.67
为什么当我设置 "hex" 和 "oct" 标志时,它们不起作用?
在文本文件中,我期待 "hexadecimal formal: " 和 "octal format: " 旁边的“3591.67”以外的内容。
我是否实施了错误的标志?
不幸的是,八进制和十六进制打印仅适用于整数,而不适用于双精度。参见 http://stdcxx.apache.org/doc/stdlibug/28-3.html
如果你想使用setf,应该是:
outfile.setf(ios_base::hex,ios_base::basefield);
。或者,输入 std:hex,即:
outfile << std::hex;
.
八进制和十六进制格式仅影响整数的显示方式。如果您想查看十六进制浮点数,您可以使用 hexfloat
(C++11) 或使用 cstdio
中的 printf
函数和 %a
格式代码。
另见 Dump hex float in C++