0

任何人都可以告訴我,在生產者消費者問題中,如何從文件中讀取一行並將其存儲到大小爲10的緩衝區中?從文件中讀取並存入緩衝區

static char buf[10][256]; 
void *producer(void *var) 
{ 
    char line[256]; 
    int i; 
    for(;;) 
    { 
    if(feof) 
     break; 

    for(i=0;i<10, i++) 
     buf = fgets(line,256, in); // what should be the correct coding here to read from file and store in buffer ? 


    if (pushInBuffer(&buf)) 
     fprintf(stderr, "Error Consuming"); 


    } 

pthread_exit(0); 
} 
+0

'fgets(buf [i],256,in);'This post may help you http://stackoverflow.com/questions/3501338/c-read-file-line-by-line – user376507

回答

1

應該

fgets(buf[i],256, in); 
0

其他答案工作太偉大,我通常

fscanf(in, "%d", &buf[i]) 

的fscanf去只能讀取,直到空白的,所以,最好只有使用它,你知道讀取文件的結構(例如fscanf(fp,「%d%d%s」,& i1,& i2,string),在我看來通常比較小巧