2014-03-25 4 views
0

전 세계적으로 작성한 파일에 쓸 수있는 문제가 있습니다. 주 (성공적으로) 초기화되고 여러 스레드에서 사용하는 함수에 쓰기 (on 리눅스).C에서 스레드로 글로벌 파일에 쓰기

#includes 

FILE *f; 
main(){ 
    // Create threads successfully 
    f = fopen("fileName.txt", "w"); 
    // Make sure the file was able to be created 
    if(f = NULL){ 
      printf("Unable to create file"); 
      exit(1); 
    } 
    // This much works, the check indicates the file was created 
    // successfully when I run it 
    while(1){ 
     // loops for a while, getting input from user to direct threads 
     // When end is determined, waits for all the threads to finish, 
     // clears allocated memory, and closes file then returns 
     fclose(f); 
     return; 
    } 


} 

void *threadProcess(){ 
    // Do stuff 
    // This printf works fine using the values i give the function, as is here 
    // The values are determined in 'Do stuff' 
    printf("%d trying to write \"%d BAL %d TIME %d.%06d %d.%06d\" to the file\n", cid, tmp->reqNum, balance, tmp->seconds, tmp->useconds, endTime.tv_sec, endTime.tv_usec); 
    fflush(stdout); 

    // There appears to be a Segmentation fault here 
    fprintf(f, "%d BAL %d TIME %d.%06d %d.%06d\n", tmp->reqNum, balance, tmp->seconds, tmp->useconds, endTime.tv_sec, endTime.tv_usec); 
    // Never gets here 
} 

내가 뭘 잘못하고 있니? 앞에서 설명한 것처럼 fprintf 문 바로 앞에있는 printf 문은 올바르게 작동합니다.

나는 fprintf에 대한 포인터 문제가 없는지 확신하지 못하고 있습니까? 내 if(reqLog = NULL) 점검했다

감사

+1

거의 모든 코드를 생략했습니다. 아마도 문제는 우리가 볼 수없는 코드에 있습니다. –

+0

@DavidHeffernan 그렇습니다. 약 600 줄의 코드이기 때문입니다. 내 모든 테스트가 fprintf를 이끌어 냈기 때문에 다른 코드가 있다고 생각하지 않습니다. 또한 뮤텍스를 사용하여 문을 잠그려고했는데 문제가 해결되지 않았습니다. ** threadProcess **는 단지 도우미 기능입니다. 내가 쓰기/글로벌 파일을 올바르게 사용하고 있습니까? – tompon

+0

그럼 잘 해봐. 내가 너라면 SSCCE를 만들거야. –

답변

0

는 .... 나는 비교되지 할당되었다. 하하, 시간 낭비해서 죄송합니다. - 톰폰