• Advertisement
Sign in to follow this  

Running command lines from c++

This topic is 4709 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
system("<command>")

Maybe a better way of doing it, but that's the first thing that comes to mind.

(newbies use this all the time without realizing that "PAUSE" actually executes the command-line function PAUSE.)

[insert disclaimer here]

Share this post


Link to post
Share on other sites
Quote:
Original post by squicklid
ok rad, and now how do i get the output from that?


there may be a better way, but you could redirect the output to a file with "> filename".

#include <cstdlib> // pretty sure this is where system() is
system( "dir > temp" ); // then open the file "temp" and read it


Share this post


Link to post
Share on other sites
On a POSIX compliant system, you'd create a pipe using the popen() function, then read from that using the standard file input functions (fread(), fgets(), fscanf() etc.) and finally close the stream with pclose(). Something like this:

#include <stdio.h>

int main()
{
// Open a pipe for reading ("r")
FILE *list = popen("ls -l", "r");

char line[80];
// Just echo the lines as they come
while(fgets(line, 80, list))
fputs(line, stdout);

// close the pipe
pclose(list);
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement