something about system fucntion

i want to put all the contains returned by system("dir") into a text file.
and also i wanna avoid the output on the screen (console program) when i use the system().
and please don't suggest system("cls").

how can i do this.
By "dir" I assume you're using Windows. If you are, you'll need to FindFirstFile ...

Docs and an example are here: http://msdn.microsoft.com/en-us/library/aa364418%28v=vs.85%29.aspx

You should not use system() when you have alternatives in c. This is not a recommended way of interacting with the system.

If you want to list all the directories or files.

for unix:
http://linux.die.net/man/3/opendir


for windows:
http://msdn.microsoft.com/en-us/library/aa364418%28VS.85%29.aspx


read about these functions and they will do it for you.
i know how to use _findfirst and _findnext and i can also create my own program to list dirs.

but i want to pipe the buffers and etc returned by the system function into the text file.

i am asking this question because not only i wanna use system("dir") but i will use this on other commands.

don't worry about security i use system() when i am in hurry and not doing any important work.

@kbw, our posts are clashing.. :P

But you can write to file whatever is returned from these functions. I mean you can achieve everything what a system("dir") can for you.

Although, i have no idea how will you catch anything returned by system().
Last edited on
Here I go again with can't. You can't.

system() spawns the shell, which runs the command. The command in this case, isn't even an external program, it's a shell directive.

You can control stdin/stdout on the programs that your app spawns and you can deal with redirection yourself, but not by using system().

I've never seen system() used in any commercial app and I suggest you consider not using it at all.
Topic archived. No new replies allowed.