Jump to content
  • Advertisement
Sign in to follow this  
squicklid

Running command lines from c++

This topic is 4829 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
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!