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

58 Views Asked by At

I'm using linux manjaro. I have the following code (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.

1

There are 1 best solutions below

0
NoComprende On

The problem lies with the interactive debuggers in VS Code. It works fine when debugging using gdb in a VS Code terminal. When using the interactive debuggers in VS code the second reopen returns null. Thanks to John Bollinger.