ファイルを読み込んで処理し、結果を出力ファイルに入れるプログラムがあります。引数(入力ファイル)があると、出力ファイルを作成して内容を書き出します。execl子どもから離れることはありません
stdoutにwrite()コンテンツをリダイレクトするためにfork()を行っています。
char *program;
program = malloc(80);
sprintf(program, "./program < %s > %s", inputFile, outputFile);
int st;
switch (fork()) {
case -1:
error("fork error");
case 0:
/* I close the stdout */
close (1);
if ((fd = open(outputfile, O_WRONLY | O_CREAT , S_IWUSR | S_IRUSR | S_IRGRP)==-1)){
error("error creating the file \n");
exit(1);
}
execlp("./program", program, (char *)0);
error("Error executing program\n");
default:
// parent process - waits for child's end and ends
wait(&st);
exit(0);
//*****************
}
子供が<> stdinとstdoutをファイルが作成されると正しく作成されます。 しかし、子供は決して終わらず、私が父親を殺すと、出力ファイルは空になるので、コードは実行されませんでした。
何が起こっていますか? ありがとう!
リダイレクション。 – Flexo
ありがとう!両方のソリューションが機能しました。 – drules