?
Solved

How to execute a command and save the results ??

Posted on 2004-05-03
8
Medium Priority
?
452 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 100 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
AWS Certified Solutions Architect - Associate

This course has been developed to provide you with the requisite knowledge to not only pass the AWS CSA certification exam but also gain the hands-on experience required to become a qualified AWS Solutions architect working in a real-world environment.

 
LVL 35

Assisted Solution

by:Duncan Roe
Duncan Roe earned 100 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

How to Create Failover DNS Record Sets in Route 53

Route 53 has the ability to easily configure DNS record sets specifically for failover scenarios. These failover record sets can be configured to failover to full-blown deployments in other regions or to a static HTML page that informs your customers of the issue.

Question has a verified solution.

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

If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
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…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Suggested Courses
Course of the Month11 days, 23 hours left to enroll

752 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