468,512 Members | 1,423 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

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:
Expand|Select|Wrap|Line Numbers
  1. FILE *File = NULL;
  2. freopen_s(&File, "somefile.txt", "w", stdout);
  3. system ("dir");
  4. fclose(stdout);
...but this doesn't help. Is there any way to do this?

Thank you.
Mar 21 '08 #1
2 4366
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:
Expand|Select|Wrap|Line Numbers
  1. 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?
Mar 21 '08 #2
weaknessforcats
9,207 Expert Mod 8TB
Why not just call the same functions that dir does and write your output to a file?
Mar 21 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by Hal Vaughan | last post: by
4 posts views Thread by Paul Nilsson | last post: by
1 post views Thread by iksrazal | last post: by
1 post views Thread by Peter Kirk | last post: by
2 posts views Thread by Zeba | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.