简体   繁体   English

使用 freopen 重定向标准输出的简单 c 应用程序中的调试和运行结果不同

[英]Debug & run result differ in simple c app that redirects stdout using freopen

I'm using linux manjaro.我正在使用 linux manjaro。 I have the following code (cons.c)我有以下代码(cons.c)

#include <stdio.h>
#include <stdlib.h>
int main()
{
    FILE *f=freopen("/dev/null", "a", stdout); //redirect stdout to /dev/null
    FILE *g=freopen ("/dev/tty", "a", stdout); // redirect stdout back to console
    printf("%p %p\n",f,g);
    return 1;
}

I executed the following and got the expected result我执行了以下操作并得到了预期的结果

[tom@sp4 src]$ gcc -g cons.c -o cons
[tom@sp4 src]$ ./cons
0x7f19897b7520 0x7f19897b7520

When I set a breakpoint on the bottom line and debug with the VS Code interactive debuggers however g returns null and the printf does nothing.当我在底线设置断点并使用 VS Code 交互式调试器进行调试时,g 返回 null 而 printf 什么也不做。

The problem lies with the interactive debuggers in VS Code.问题在于 VS Code 中的交互式调试器。 It works fine when debugging using gdb in a VS Code terminal.在 VS Code 终端中使用 gdb 进行调试时,它工作正常。 When using the interactive debuggers in VS code the second reopen returns null.在 VS 代码中使用交互式调试器时,第二次重新打开返回 null。 Thanks to John Bollinger.感谢约翰·布林格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM