c++ linux system command c++ linux system command linux linux

c++ linux system command


you cannot concatenate strings like you are trying to do. Try this:

curr_val=60;char command[256];snprintf(command, 256, "echo -n %d > /file.txt", curr_val);system(command);


The system function takes a string. In your case it's using the text *curr_val_str* rather than the contents of that variable. Rather than using sprintf to just generate the number, use it to generate the entire system command that you require, i.e.

sprintf(command, "echo -n %d > /file.txt", curr_val);

first ensuring that command is large enough.


The command that is actually (erroneously) executed in your case is:

 "echo -n curr_val_str  > /file.txt"

Instead, you should do:

char full_command[256];sprintf(full_command,"echo -n  %d  > /file.txt",curr_val);system(full_command);