简体   繁体   中英

How can I communicate with Command Prompt (CMD) using C++?

I have an application that need to send command to cmd and then get the ouput back (capturing the output). How can this be accomplished using C++ without using any MS Windows specific API? Is there a way that this may be done to be cross platform (for linux terminals for example)?. By the way i'm on win XP SP3.

I actually mean redirecting the input/output. For example, run the command "make" on cmd and then in case of error capturing the error message (redirecting to my application).

As mentioned: if you can avoid launching child processes in your program and instead fit into the broader "toolbox metaphor," that can often be better...

http://en.wikipedia.org/wiki/Unix_philosophy

But if that's not a fit for your project, check out Boost.Process .

Also: if you're using Qt (which is good to look at in any case) there is also QProcess .

Can't you just use the regular cin and cout that C++ provides? (of course if your program is a GUI program, cin and cout won't be connected to anything useful unless you call the Windows AllocConsole() command... but that's just the way Windows works. If you want code that also compiles under Linux, etc, you can put #ifdef WIN32 around that call)

系统()函数是C89和C99标准的一部分,可在Linux和Windows上使用,并允许在C / C ++中执行命令。

By default, most systems get their standard input from the keyboard, therefore cin is generally expected to get information from the user, although there are many cases where this can be redirected to some other source.

You can do something like:

cout << "How old are you?" << endl;
int age;
cin >> age;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM