Redirect system() output?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • loketing
    New Member
    • Mar 2008
    • 2

    Redirect system() output?

    Is there any way to redirect the output of the system() function? I know there is for redirecting stdout, but I can't make this work with system().

    I tried to store the output of a system command in a file, like so:
    Code:
    FILE *File = NULL;
    freopen_s(&File, "somefile.txt", "w", stdout);
    system ("dir");
    fclose(stdout);
    ...but this doesn't help. Is there any way to do this?

    Thank you.
    Last edited by loketing; Mar 21 '08, 01:17 PM. Reason: Undescriptive title
  • loketing
    New Member
    • Mar 2008
    • 2

    #2
    Ok, so I found one way that sort of helped. By just adding "<< somefile.txt" after the system call, I could capture the output. Like so:
    Code:
    system("dir >> somefile.txt");
    However, this only partly solves my problem. A console window still opens, and executes the command before it is stored in a file. I need to disable this window from appearing as well, is there any way to do this?

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Why not just call the same functions that dir does and write your output to a file?

      Comment

      Working...