2

I need to have an input that looks like

./a.out <exe> <arg1> ... <argn> <others_stuff>

where <exe> <arg1> ... <argn> is the input that I must execute as a separate process (the objective is to save the exe's output into a txt).

Saving output into a txt file isn't a problem, I just have to redirect stdout (using dup2, freopen, or something similar).

The problem is to execute just a portion of argv! Because exec's family functions (they are so many!) let to give as input whole argv, or specifying each arg. I'm writing over here because I can't solve the problem, so I hope you're going to help me (I googled everywhere with no success).

EDIT: I forgot to say that i cannot use system for execute the command!

4

2 に答える 2

3

argvの連続部分を使用する場合は、2つのオプションがあります。(試したように)新しいarg配列を作成し、次のように適切に入力できます。

  char *params[argc-2];
  memcpy(params, argv+1, sizeof params);
  params[argc-3] = NULL;

  execvp(*params, params);

あなたはただ粉砕することができますargv

  argv[argc-3] = NULL;
  execvp(argv[1], argv+1);

または、引数が多すぎない場合は、次を使用できますexeclp

  execlp(argv[0], argv[0], argv[3], argv[2], argv[4], NULL);
于 2012-05-28T13:56:29.603 に答える
1

引数をポインタで終了する配列としてexec受け入れるため、既存のメンバーを使用して、最後に渡したいメンバーの後にメンバーを設定できます。argvchar*NULLargvNULL

これは破壊しargvます-それが問題である場合は、最初にそれをコピーすることができます(新しいコピーのためにいくらかのメモリを割り当てる必要があります...)

于 2012-05-28T13:01:10.767 に答える