C语言 如何在 C 中打印到 stderr?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39002052/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 10:27:50  来源:igfitidea点击:

How I can print to stderr in C?

cprintfstderr

提问by wad

In C, Printing to stdout is easy, with printffrom stdio.h.

在 C 中,使用printffrom打印到标准输出很容易stdio.h

However, how can print to stderr? We can use fprintfto achieve it apparently, but its syntax seems strange. Maybe we can use printfto print to stderr?

但是,如何打印到stderr?我们fprintf显然可以使用来实现它,但它的语法似乎很奇怪。也许我们可以printf用来打印到标准错误?

回答by Fantastic Mr Fox

The syntax is almost the same as printf. With printfyou give the string format and its contents ie:

语法几乎与printf. 与printf您一起提供字符串格式及其内容,即:

printf("my %s has %d chars\n", "string format", 30);

With fprintfit is the same, except now you are also specifying the place to print to:

fprintf它相同,除了现在您还指定要打印到的位置:

File *myFile;
...
fprintf( myFile, "my %s has %d chars\n", "string format", 30);

Or in your case:

或者在你的情况下:

fprintf( stderr, "my %s has %d chars\n", "string format", 30);

回答by Paul R

Examples:

例子:

printf("%s", "Hello world\n");              // "Hello world" on stdout (using printf)
fprintf(stdout, "%s", "Hello world\n");     // "Hello world" on stdout (using fprintf)
fprintf(stderr, "%s", "Stack overflow!\n"); // Error message on stderr (using fprintf)

回答by hasectic saif

#include<stdio.h>

int main ( ) {
    printf( "hello " );
    fprintf( stderr, "HELP!" );
    printf( " world\n" );
    return 0;
}

$ ./a.exe
HELP!hello  world
$ ./a.exe 2> tmp1
hello  world
$ ./a.exe 1> tmp1
HELP!$
  1. stderr is usually unbuffered and stdout usually is. This can lead to odd looking output like this, which suggests code is executing in the wrong order. It isn't, it's just that the stdout buffer has yet to be flushed. Redirected or piped streams would of course not see this interleave as they would normally only see the output of stdout only or stderr only.

  2. Although initially both stdout and stderr come to the console, both are separate and can be individually redirected.

  1. stderr 通常是无缓冲的,而 stdout 通常是。这可能会导致像这样看起来很奇怪的输出,这表明代码以错误的顺序执行。不是,只是标准输出缓冲区尚未刷新。重定向或管道传输的流当然不会看到这种交错,因为它们通常只会看到 stdout 或 stderr 的输出。

  2. 尽管最初 stdout 和 stderr 都进入控制台,但两者都是分开的,可以单独重定向。

回答by Some programmer dude

Do you know sprintf? It's basically the same thing with fprintf. The first argument is the destination (the file in the case of fprintfi.e. stderr), the second argument is the format string, and the rest are the arguments as usual.

你知道sprintf吗?与fprintf. 第一个参数是目标(在fprintfie的情况下是文件stderr),第二个参数是格式字符串,其余的都是参数。

I also recommend this printf(and family) reference.

我也推荐这个printf(和家庭)参考

回答by lxiange

If you don't want to modify current codes and just for debug usage.

如果您不想修改当前代码而只是为了调试使用。

Add this macro:

添加这个宏:

#define printf(args...) fprintf(stderr, ##args)
//under GCC
#define printf(args...) fprintf(stderr, __VA_ARGS__)
//under MSVC

Change stderrto stdoutif you want to roll back.

如果要回滚,请更改stderrstdout

It's helpful for debug, but it's not a good practice.

这对调试很有帮助,但这不是一个好习惯。

回答by winterli

To print your context ,you can write code like this :

要打印您的上下文,您可以编写如下代码:

FILE *fp;
char *of;
sprintf(of,"%s%s",text1,text2);
fp=fopen(of,'w');
fprintf(fp,"your print line");