-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 :)

4
  • 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 Commented Aug 13, 2014 at 22:55
  • That isn't how you appends strings, see this answer on how to do that in C. stackoverflow.com/questions/5901181/c-string-append Commented Aug 13, 2014 at 22:56
  • 2
    I think he's trying to pipe into a C array from a system command? That doesn't work either. Commented Aug 13, 2014 at 22:57
  • output is a empty string at best and garbage at worst. What's the purpose of this array? Commented Aug 13, 2014 at 22:57

1 Answer 1

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;
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.