Solved

How to execute a command and save the results ??

Posted on 2004-05-03
8
417 Views
Last Modified: 2008-02-26
Simple:

I want to execute the command "ls" in my mandrake 10.

I have a variable "string command;", so I want that "command" gets the result of the "ls" command.

Something like this:

string command;  // Variable
command = // Whatever is under "ls"
cout << command; // This should print home  folders  etc...
0
Comment
Question by:digi_mind
8 Comments
 
LVL 12

Expert Comment

by:stefan73
ID: 10978770
Hi digi_mind,
What you're looking for is the popen command:

#include <stdio.h>

FILE* ls_output = popen("ls", "r");
[...]
pclose(ls_output);

...but unfortunately, this uses C-style, buffered IO. I'm not sure if there is a constructor for an istream which takes a FILE* as argument. Try it out.

Cheers,
Stefan
0
 
LVL 12

Accepted Solution

by:
stefan73 earned 25 total points
ID: 10978899
Try this:

#include <stdio.h>

main(){
    char buffer[1024];
    string command;
    size_t chars;

    FILE* ls_output = popen("ls", "r");
    while(chars = fread(buffer,1,sizeof(buffer),ls_output))
        command.append(buffer,chars);    

    pclose(ls_output);

    cout << command;

}
0
 

Author Comment

by:digi_mind
ID: 10984364
I have this to execute a specific command in Linux

#include <stdio.h>

main(){
    char buffer[1024];
    string command;
    size_t chars;

    FILE* ls_output = popen("ls", "r");
    while(chars = fread(buffer,1,sizeof(buffer),ls_output))
        command.append(buffer,chars);    

    pclose(ls_output);

    cout << command;

}

what can I do to execute any command not only the one I specify, for example I have in a string called request command, which contains any command that a user types, I want that command to be executed and the result displayed.
How can I do that ?
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 34

Assisted Solution

by:Duncan Roe
Duncan Roe earned 25 total points
ID: 10984906
Cheap & cheerful solution:
Pass the command in quotes, e.g.:-
myprog "ls -A -Fx"

Now change your above program line 8 to be 'FILE* ls_output = popen(argv[1], "r");'
0
 

Author Comment

by:digi_mind
ID: 10984965
Ok, I will try to ask in a simplier way:

This is my program:
*******************
string command;
cin >> command;
FILE * test = popen(command , "r");
***********************************
This does not work... and I do not know why. Any suggestion ???

0
 
LVL 1

Expert Comment

by:arn0ld
ID: 10992114
how about a
 cout<<command<<endl;
after cin?

You are calling  MyProgram as
echo ls|MyProgram
not as
MyProgram ls  
?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Edit linux file using python 4 40
Fixing Non-Standard characters in text 8 93
Steps to follow for Maintenance of Servers 4 54
how to rebuild XFS volume from LV 19 40
SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now