【问题标题】:Why are std::hex and std::oct flags not working?为什么 std::hex 和 std::oct 标志不起作用?
【发布时间】:2015-11-08 21:08:28
【问题描述】:

这是我的代码:

// 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”标志时,它们不起作用?

在文本文件中,我期望在“十六进制形式:”和“八进制格式:”旁边出现“3591.67”以外的内容。

我是否错误地实现了标志?

【问题讨论】:

    标签: c++ hex iostream flags


    【解决方案1】:

    不幸的是,八进制和十六进制打印仅适用于整数,而不适用于双精度数。见http://stdcxx.apache.org/doc/stdlibug/28-3.html

    如果你想使用 setf,它应该是:

    outfile.setf(ios_base::hex,ios_base::basefield);
    

    。或者,在 std:hex 中使用管道,即:

    outfile << std::hex;
    

    .

    【讨论】:

    • 我将变量“number”改为“int”类型,问题依旧存在...
    • 试试 outfile.setf(ios_base::hex, ios_base::basefield) ?
    • setf 被重载来做两件不同的事情。 setf(x) 或 x 与格式标志,而 setf(x,y) 将格式标志设置为 (x&y)。所以它不起作用的原因可能是因为设置了一些其他格式标志,比如科学记数法中的标志,但没有被 setf“清除”,所以它的行为不像预期的那样。
    【解决方案2】:

    八进制和十六进制格式仅影响整数的显示方式。如果您想查看十六进制浮点数,您可以使用hexfloat (C++11) 或使用cstdio 中的printf 函数和%a 格式化代码。

    另见Dump hex float in C++

    【讨论】:

    • flags.cpp:在函数“int main()”中:flags.cpp:37:16:错误:“hexfloat”不是“std::ios_base”的成员 outfile.setf(ios_base ::hexfloat); // 数字将以十六进制格式显示。 ^
    • hexfloat 是 c++11 标准的一部分。编译器是否设置为使用它?对于g++,我认为你需要添加--std=c++11。
    • 不...仍然无法正常工作... g++ flags.cpp -o flags --std=c++11
    • @Evan:这取决于您的编译器版本。他们没有一次性实现所有 C++11。 gcc.gnu.org/bugzilla/show_bug.cgi?id=59987
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多