std::cout 在 QNX 中不打印 \n 之后的字母

std::cout doesn't print letters after \n in QNX

我想打印存储在单个字符串中的项目列表。我不知道为什么最后一项被跳过了!

你知道为什么吗?

#include <iostream>
#include <string>

int main() {
  std::string string_test{"Item1\nItem2\nItem3"};
  std::cout << string_test;

  return 0;
}

输出为:

Item1
Item2

在 POSIX 2017 年,C++ 实现不需要 在程序终止时刷新程序的标准输出1。你的没有。您需要手动完成:

#include <iostream>
#include <string>

int main()
{
    std::string string_test{"Item1\nItem2\nItem3"};
    std::cout << string_test << std::flush;
}

1)

The Open Group Base Specifications Issue 7, 2018 edition

As required by the ISO C standard, using return from main() has the same behavior (other than with respect to language scope issues) as calling exit() with the returned value. Reaching the end of the main() function has the same behavior as calling exit(0).

[...]

The _Exit() and _exit() functions shall not call functions registered with atexit() nor any registered signal handlers. [Option Start] Open streams shall not be flushed. [Option End] Whether open streams are closed (without flushing) is implementation-defined. Finally, the calling process shall be terminated with the consequences described below.

如果您可以使用 std::cerr 而不是 std::cout,在 std::cerr 上刷新是自动的

#include <iostream>
#include <string>

int main()
{
    std::string string_test{"Item1\nItem2\nItem3"};
    std::cerr << string_test;

    return 0;
}

这是 QNX 版本 7.0.3 上行缓冲流输出中报告的错误。

Source