By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,484 Members | 1,553 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,484 IT Pros & Developers. It's quick & easy.

command output

P: n/a
Can someone give me a help? A need to create a program, that will
simulate imput and output from command line. For example, I write in
program dir "c:\program files\" and in the screen appear:

15.11.2005 12:06 <DIR> .
15.11.2005 12:06 <DIR> ..
15.11.2005 12:06 <DIR> MSN
15.11.2005 12:06 <DIR> TEMP
..
..
..

Thax

Nov 22 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
peter....@orangemail.sk wrote:
Can someone give me a help? A need to create a program, that will
simulate imput and output from command line. For example, I write in
program dir "c:\program files\" and in the screen appear:

15.11.2005 12:06 <DIR> .
15.11.2005 12:06 <DIR> ..
15.11.2005 12:06 <DIR> MSN
15.11.2005 12:06 <DIR> TEMP


You'll have to

1) define a string
std::string command;

2) read some input
getline(std::cin, command);

3) send that input to the system
system(command.c_str);

Note that what system() does is implementation defined. For example,

system("dir c:");

will behave differently on Windows and on Linux.
Jonathan

Nov 22 '05 #2

P: n/a

Jonathan Mcdougall wrote:
3) send that input to the system
system(command.c_str);


I'm sure it was just a typo, but you mean

system(command.c_str());

Gavin Deane

Nov 22 '05 #3

P: n/a
de*********@hotmail.com wrote:
Jonathan Mcdougall wrote:
3) send that input to the system
system(command.c_str);


I'm sure it was just a typo, but you mean

system(command.c_str());


Still early here, you know... :)
Jonathan

Nov 22 '05 #4

P: n/a
But how would he then capture that output into his string?
For example, he calls

system("dir C:");

The directory will be displayed in the command window, but can he
capture this directory
into a string?

Like:
string mystring = system("dir C:");

Nov 22 '05 #5

P: n/a
adamrobill...@gmail.com wrote:
But how would he then capture that output into his string?
For example, he calls

system("dir C:");

The directory will be displayed in the command window, but can he
capture this directory
into a string?

Like:
string mystring = system("dir C:");


Exactly what system() does is implementation defined, so redirecting
the output is again implementation defined. However, most OS support
redirection ("pipes"), so under windows you could do

system("dir c: > temp.txt");

and read the content of temp.txt. It may also be possible to redirect
the output stream of the console, but again, this is implementation
defined. On Windows, SetStdHandle() may be what you are searching for.
Jonathan

Nov 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.