-5
shell_command(char gcommand[100]) {
 char output[100];
 system(gcommand ">" output);

 return output;
}

Gives me
error: expected ‘)’ before string constant

I am not quite sure why this happens. Appriciate any help :)

user2864740
  • 60,010
  • 15
  • 145
  • 220
user3760230
  • 29
  • 1
  • 5
  • 6
    What are you expecting `system(gcommand ">" output);` to do? It's simply wrong C. Fix that and I bet you that your problem will disappear – Avery Aug 13 '14 at 22:55
  • That isn't how you appends strings, see this answer on how to do that in C. http://stackoverflow.com/questions/5901181/c-string-append – Adam Aug 13 '14 at 22:56
  • 2
    I think he's trying to pipe into a C array from a system command? That doesn't work either. – Avery Aug 13 '14 at 22:57
  • `output` is a empty string at best and garbage at worst. What's the purpose of this array? – Fiddling Bits Aug 13 '14 at 22:57

1 Answers1

1

String literals can be concatenated like that, but string values cannot.

Further, it seems that you want the output of gcommand to end up in the buffer output.

It is not possible to do that with the system function. Assuming you are going to be executing in a POSIX-style shell where > is the shell redirection operator, the thing to the right of it must be a file name (or descriptor) in the shell.

To execute a command and capture the output, one way is to use the POSIX popen function:

FILE *pipe = popen(gcommand, "r");
char output[100] = { 0 };

if ( pipe )
{
    fgets(output, sizeof output, pipe);
    pclose(pipe);
}

return output;
M.M
  • 138,810
  • 21
  • 208
  • 365