Using system function in C

Hi Guys ,

I want to use system function in C to do the following work.

cp <file1> <file2> and then ><file1>
e,g cp \var\log\cpm_cpmd_1.log.1 \var\log\cpm_cpmd_1.log.2 and then
>\var\log\cpm_cpmd_1.log.1

  1. g_config_info.cpmm_config.cpm_log_path=\var\log\
  2. p_g_log_limit_info[thread_id]->file_name= cpm_cpmd_1.log

system("cp (g_config_info.cpmm_config.cpm_log_path)+ (p_g_log_limit_info[thread_id]->file_name)+'.'+'1' (g_config_info.cpmm_config.cpm_log_path)+(p_g_log_limit_info[thread_id]->file_name)+'.'+'2'");

I am doing like this but it is not working . It seems the command being executed is
cp \var\log\cpm_cpmd_1.log.1\var\log\cpm_cpmd_1.log.2

CAn anybody help .

regards
Aki

Okay. You can't insert variables inside strings like that because C is not a shell language. You have to create the string first, then call system with it. It's probably simplest to use sprintf.

char buf[1024];
// You can stick together "strings" "like" "this" to get "stringslikethis" which lets you 
// seperate one string across lines, like I'm doing with the two echo commands.
sprintf(buf, "echo '%s' ;"
             "echo '%s'",
        "string 1 which must not contain quote characters",
        "string 2 which must not contain quote characters");

fprintf(stderr, "calling system('%s')\n", buf);
system(buf);

Thanks for the information . i have one more query , i am running this above function quite frequently ,Is it possible to run this in background . So that the command does not apper in bash shell

regadrs
Akhilesh

When you're using 'system', you're running a shell. You can put redirection and background in the string just as in a shell.