0

重複の可能性:
「while( !feof( file ) )」は常に間違っています

while ループに関連する奇妙な問題があります。プロセス ( ) の親の最後で呼び出される関数があり、print_file()先に進むための真の条件を受け入れません。以下に示すように、これは私の単純なマルチプロセス コードです。

#include <stdio.h>     /* basic I/O routines.   */
#include <stdlib.h>
#include <unistd.h>    /* define fork(), etc.   */
#include <sys/types.h> /* define pid_t, etc.    */
#include <sys/wait.h>  /* define wait(), etc.   */
#include <signal.h>    /* define signal(), etc. */
#include <pthread.h>
#include <time.h>
#include <ctype.h>

void print_file(char* [], char* []);
void child_process(int,int);
void parent_process();
int counter=0;

int main(int argc, char* argv[]) {

    counter = atoi(argv[1]);
    int i,k;
    pid_t child_pid;
    int child_status;
    char* array[counter];
    srand ( time(NULL) );
    int temp;

    for(i=0; i<counter; i++){
        temp = rand()%4;
        child_pid = fork();

        switch(child_pid) {
            case -1:
                printf("Error occured with fork()\n");
                exit(1);
            case 0: 
                child_process(i,temp); /* Child Process */
                exit(0);
        }
    }

    wait(&child_status);
    parent_process();
    execl("/usr/bin/killall","killall","tail",(char *) 0);
    return 0;
}

void child_process(int i,int temp){

    FILE* fptr;
    fptr = fopen("sample.txt","a+");
    if( temp==0 ) {
        fprintf(fptr,"A %d\n",i);
    }
    else if( temp==1 ) {
        fprintf(fptr,"C %d\n",i);
    }
    else if( temp==2 ) {
        fprintf(fptr,"G %d\n",i);
    }
    else if( temp==3 ) {
        fprintf(fptr,"T %d\n",i);
    }
    fflush(fptr);
    sleep(1);
    fclose(fptr);
}

void parent_process(void){

    FILE* fptr;
    fptr = fopen("sample.txt","r");
    char* str = (char*)malloc(1);
    int temp,i,k;
    char* array_opst[counter];
    char* array[counter];

    i=0;
    while(!feof(fptr)){

        fscanf(fptr,"%s%d",str,&temp);
        if(feof(fptr))
            break;

        if(strcmp(str,"A")==0){
            array[temp]="A";
            array_opst[temp]="T";
            printf("Array[%d] = %s\n",temp,array[temp]);
        }
        else if(strcmp(str,"C")==0){
            array[temp]="C";
            array_opst[temp]="G";
            printf("Array[%d] = %s\n",temp,array[temp]);
        }
        else if(strcmp(str,"G")==0){
            array[temp]="G";
            array_opst[temp]="C";
            printf("Array[%d] = %s\n",temp,array[temp]);
        }
        else if(strcmp(str,"T")==0){
            array[temp]="T";
            array_opst[temp]="A";
            printf("Array[%d] = %s\n",temp,array[temp]);
        }
        i++;
    }
    fclose(fptr);
    free(str);
    print_file(array,array_opst);
}

void print_file(char* array[counter], char* array_opst[counter]) {

    int j=0,i=1;

    while(j<counter){
        printf("%d", i);
        i++;
        j++;
        if(i==10){
            i=0;
        }
    }
    return;
}

関数内ではprint_file条件成立時でもwhileループには入らない。しかし、関数の最初の行にprintfを配置して、print_file正常に入力されたかどうかを確認すると、常に印刷されます。counter(これはグローバル変数であることに注意してください)。この問題の原因は何ですか?

4

1 に答える 1

5

ループからの出力はstdoutバッファリングされているため (printf()ループ内のステートメントには no が含まれており、明示的にd\nではない) 、現在のプロセス イメージを置き換えるinへの呼び出しが表示されず、バッファは通常のプログラム終了時に通常どおりフラッシュされません。 . ループの後にor 明示的に呼び出しに改行文字を追加します。stdoutfflush()execl()main()stdoutprintf()fflush(stdout);


未定義の動作を引き起こすバッファ オーバーランがあります。

char* str = (char*)malloc(1);

1 バイトを割り当て、次のstrように使用されます。

fscanf(fptr,"%s%d",str,&temp);

fscanf()str1 つしか読み取られない場合でも、末尾をオーバーランするヌル ターミネータを追加しますchar。これを動的に割り当てる理由がわかりません。また、行の形式が単一のchar後に続くように見えるため、配列にする必要がある場合でも、理由がわかりませんint

fprintf(fptr,"A %d\n",i);

この問題を解決するには、代わりにacharと書式指定子を使用します。%c

char ch;

if (2 == fscanf(fptr, "%c%d", &ch, &temp))
{
}

常に戻り値 ( 、 など) を確認しfopen()fprintf()くださいfscanf()

于 2012-10-10T21:26:56.530 に答える