stdout和stderr
In C++, how to print a string as a line to STDOUT? That is, the string and the newline character, nicely?
在C ++中,如何打印一个字符串作为一行STDOUT ? 也就是说,字符串和换行符很好吗?
And similarly, how to print the line to STDERR?
同样,如何将行打印到STDERR?
In C++, you may print the string and then 'n'
or std::endl
to STDOUT by operating on the std::cout
stream:
在C ++中 ,您可以通过对std::cout
流进行操作来打印字符串,然后将'n'
或std::endl
打印到STDOUT:
std::cout << your_string << std::endl;
or
要么
std::cout << your_string << 'n';
Example:
例:
$ cat a.cpp
#include <iostream>
int main()
{
std::cout << "hello world!" << std::endl;
std::cout << "happy Dtivl!" << 'n';
return 0;
}
$ g++ a.cpp -o a && ./a
hello world!
happy Dtivl!
In C++, std::cerr
is a stream to the STDERR.
在C ++中 , std::cerr
是STDERR的流。
You can use the common I/O operators like <<
or std::cerr
to print content to the STDERR.
您可以使用常见的I / O运算符(例如<<
或std::cerr
将内容打印到STDERR。
One example is in stderr.cc:
一个示例在stderr.cc中:
#include <iostream>
int main()
{
std::cerr << "hello world!n";
}
Built and run it:
构建并运行它:
$ g++ stderr.cc -o s && ./s
hello world!
翻译自: https://www.systutorials.com/how-to-print-a-line-to-stderr-and-stdout-in-c-2/
stdout和stderr