0

プログラムのある時点で、テキストファイルで準備したパラメーターを使用して、別のプログラム(stdinからパラメーターを取得している)を実行したいと思います。次に、プログラムの出力を別のファイルに書き込みたいと思います。

Linuxやフォークなどは初めてです。何時間も頭を壊そうとしたが成功しなかった。入力ファイルをstdinに関連付け、出力ファイルをstdoutに関連付ける必要があることを読みました。

これは私が書いたコードです:

//move on all sub-directories .....
while((indir=readdir(dir))!=NULL)
{
    pid_t pid;
    DIR *currDir;
    struct dirent *cfile;
    char *fullpath, *outpath, *outputpath;

    //ignore hidden files
    if(indir->d_name[0]=='.')
        continue;

    //create the full path of c file
    fullpath=(char*)calloc(strlen(dirpath)+strlen(indir->d_name)+1,sizeof(char));
    strcpy(fullpath,dirpath);
    fullpath=strcat(fullpath,indir->d_name);
    fullpath=strcat(fullpath,"/");
    //open current directory
    currDir=opendir(fullpath);
    //get the c file, ignore hidden files
    while((cfile=readdir(currDir))!=NULL)
    {
        if(cfile->d_name[0]!='.')
            break;
    }
    /*compile c file*/

    //child process
    if((pid=fork())==0)
    {
        fullpath=realloc(fullpath, sizeof(char)*(strlen(fullpath)+strlen(cfile->d_name)+1));
        outpath=(char*)calloc(strlen(fullpath)+strlen(OUT_NAME)+1,sizeof(char));
        strcpy(outpath,fullpath);
        strcat(fullpath,cfile->d_name);
        strcat(outpath,OUT_NAME);
        execl("/usr/bin/gcc", "/usr/bin/gcc", "-o", outpath, fullpath,NULL);
    }
    else
    {
        wait(NULL);
    }


    //create output file path
    outputpath=calloc(strlen(fullpath)+strlen(OUTPUTFILE_NAME)+1,sizeof(char));
    strcpy(outputpath,fullpath);
    strcat(outputpath,OUTPUTFILE_NAME);

    inputpath=(char*)calloc(strlen(buff)+1,sizeof(char));
    //get input file path from buffer
    for(j=0,buffIndex++;buff[buffIndex]!='\n';buffIndex++,j++)
    {
        inputpath[j]=buff[buffIndex];
    }

////Untill here works perfectly////
    //child process
    if((pid=fork())==0)
    {
        fullpath=realloc(fullpath, sizeof(char)*(strlen(fullpath)+strlen(OUT_NAME)+1));
        strcat(fullpath, OUT_NAME);
        //re-open input file and associate it with stdin
        freopen(inputpath, "r", stdin);
        //re-open output file and associate it with stdout
        freopen(outputpath, "w", stdout);
        execl(fullpath,inputpath, NULL);
    }
    else
    {
        wait(NULL);
    }

}

* inputpathは、プログラムの入力を使用して準備したファイルのフルパスです。

* outputpathは、プログラムの出力を保存したいファイルの絶対パスです。

次のエラーが発生するため、間違いなく何か間違ったことをしています。

/home/aviad/workspace/test/dean/input.txt~: file not recognized: File truncated
collect2: error: ld returned 1 exit status
/home/aviad/workspace/test/jjang/fileoutput.txt: file not recognized: File truncated
collect2: error: ld returned 1 exit status

何か助けはありますか?

4

1 に答える 1

2

ld確認するのに十分な情報が投稿にありませんが、プログラムの名前など、ビルドツールと同じ名前を選択することで噛まれているのではないかと思います。そうして実行する場合

ld 1.txt 2.txt

を実行して ldいるのではなく、GNUリンカーなどを実行している可能性が非常に高いです( /usr/bin/ld)。通常、システムディレクトリは.、存在する場合.は、の前に検索さPATHれます。lsこれにより、名前が付けられたバイナリを含むディレクトリで実行した場合lsでも、おそらくまだ必要だったなどの不快な驚きを回避できます/bin/ls

これが、プレフィックスが付いたcwdで物事を実行することをお勧めする理由./です。そのため、代わりに次のように言います。

./ld 1.txt 2.txt

そして、システムldはもはや候補ではありません。which ldとを使用して、どちらを実行するかを決定できますwhich ./ld

したがって、実際には、プログラムをまったく実行していません。これは私を含む私が知っている数人の人々を噛みました。私の最初のUNIXプログラムは「test」という名前でした;)。

編集:出力に基づいて、名前ccはより良い候補かもしれませんが、ld同じ考えです。

編集:

あなたのアップデートに基づいて、あなたはgcc結局、そしてディレクトリの内容を読むことによってあなたが呼んでいるようです。コンパイルしているファイルがファイルであるというチェックはないよう.cですが、これらのテキストファイルが存在するディレクトリでこのコードを呼び出していないことを確認しますか?

于 2013-03-23T17:58:50.873 に答える