![](/img/trans.png)
[英]Write structure to a binary file, read it from file, store in buffer, print the buffer
[英]Read from file and store into buffer
任何人都可以告訴我,在生產者消費者問題中,如何從文件中讀取一行並將其存儲到大小為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 );
}
它應該是
fgets(buf[i],256, in);
其他答案也很有效,我通常會選擇
fscanf(in, "%d", &buf[i])
fscanf只能讀取到空格,因此,只有在您知道讀取文件的結構(例如fscanf(fp,“%d%d%s”,&i1,&i2,字符串)的情況下,才最好使用它更緊湊。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.