stdout和stderr_如何在C ++中将行打印到STDERR和STDOUT?

本文介绍了在C++中如何将字符串和换行符打印到STDOUT和STDERR。通过操作流,可以使用`std::cout`配合` `或`std::endl`打印到STDOUT,而`std::cerr`则用于将内容直接输出到STDERR。示例代码和运行方法也一并给出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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!
Answered by dtivl.
dtivl回答。

翻译自: https://www.systutorials.com/how-to-print-a-line-to-stderr-and-stdout-in-c-2/

stdout和stderr

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值