2

I'm taking a Linux class and I've never done c++ before. My shell script basically does everything on its own, but I'm required to use it from a c++ program. My shell script when run from command line outputs the results I want to back to the console. When I run it from the c++ program, my program compiles and runs but I get no output. Is this because I have some error in my c++ program, or is this supposed to happen because of the way the c++ and shell script interact?

I have seen some questions about grabbing the output from the shell script and using it in the c++ program, but I don't want to do that. Literally all my c++ program does is run the shell script.

I just want the output of my shell script to show on the console. Can you help? I can post the code I'm using if needed.

C++:

#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
using namespace std;

int main (int argc, char *argv[]) {
    string arg;
    arg = string(argv[1]);
    if (argc >= 2) {
        for (int i=2; i < argc; i++) {
            string temp = string(argv[i]);
            arg=arg+" "+temp;
        }
    }
    string command;
    command = "./findName.sh "+ arg;
    //cout << command;
    system("command");

    return 0;
}
Maribeth
  • 134
  • 1
  • 2
  • 8
  • Yes, please post the code – Brian Bi Feb 18 '14 at 05:20
  • It's pretty likely that executing the shell script is just failing silently, and that's why there's no output. But nobody can say for sure if we can't see your code. – Brian Bi Feb 18 '14 at 05:21
  • 3
    It should be system(command); instead of system("command"); – androidFan Feb 18 '14 at 05:23
  • Then I get this error: main.cpp: In function ‘int main(int, char**)’: main.cpp:19:16: error: cannot convert ‘std::string {aka std::basic_string}’ to ‘const char*’ for argument ‘1’ to ‘int system(const char*)’ make: *** [main.o] Error 1 But if I comment out the system call and instead print the value of command, I get the correct string. – Maribeth Feb 18 '14 at 05:26
  • 4
    system(command.c_str());` – Duck Feb 18 '14 at 05:27
  • To get the output, refer : http://stackoverflow.com/questions/478898/how-to-execute-a-command-and-get-output-of-command-within-c – androidFan Feb 18 '14 at 05:29
  • Sweet! That worked perfectly. I'm guessing it's because command is a c++ string but system() needs a c string? – Maribeth Feb 18 '14 at 05:29
  • Yes, you got it perfectly ! – androidFan Feb 18 '14 at 05:30
  • Seems the problem get fixed, so why no one answer the question as conclusion that we can vote it. – BMW Feb 18 '14 at 09:01

1 Answers1

0

If you want to display the output real-time, you can use something like this:

FILE* outputStream;

char buffer[1024];
outputStream = popen(command,"r");

if (outputStream == NULL)
    return 1; //couldn't execute command

while (fgets(buffer, sizeof(buffer), outputStream) != NULL) {
    //read output line-by-line to buffer and display it
    std::cout << buffer << std::endl;
}

pclose(outputStream);
sweetkorn
  • 693
  • 6
  • 6