0

i'm trying to pipe to another program on OS X with C to run a command and get results. The problem i'm having is that when the popen() function runs, the program it targets just prints results to the terminal connected to my IDE instead of the pipe which i opened.

Here's the test program

void checkcmdline(void) {
    FILE* pipe;
    
    pipe = popen("vcpkg", "r");
    while (!feof(pipe)) {
        int32_t c;
        
        c = fgetc(pipe);
        fprintf(stdout, "Character read from the pipe is %c.\r\n", (char)c);
    }
    pclose(pipe);
}

When it runs this is printed to the terminal of xcode

sh: vcpkg: command not found 
Character read from the pipe is \377.
Program ended with exit code: 0

See that line 1 is the results of the popen("vcpkg", "r") function, instead of being read inside the loop it just gets printed straight to the terminal. Anyone know why this is happening?

Richard Bamford
  • 1,835
  • 3
  • 15
  • 19
  • 2
    The first line of output comes from the command-line shell writing to the standard error stream. It informs you there is no `vcpkg` in the paths searched by the command-line shell. If `vcpkg` is present on your system, change `"vcpkg"` to the full path. If `vcpkg` is not present on your system, install it. – Eric Postpischil Feb 23 '23 at 11:59
  • 1
    Aside: [Why is “while( !feof(file) )” always wrong?](https://stackoverflow.com/q/5431941/2505965) – Oka Feb 23 '23 at 12:05
  • @EricPostpischil weirdly vcpkg is installed and typing "vcpkg" into the terminal runs vcpk fine, strange? – Richard Bamford Feb 23 '23 at 18:57

0 Answers0