0

Could anyone please tell me that in producer consumer problem, How one can read a line from file and store into buffer of size 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 );
}
crashmstr
  • 28,043
  • 9
  • 61
  • 79
denizen
  • 458
  • 1
  • 5
  • 15
  • `fgets(buf[i],256, in);` This post might help you http://stackoverflow.com/questions/3501338/c-read-file-line-by-line – user376507 Feb 21 '14 at 20:09

2 Answers2

1

It should be

  fgets(buf[i],256, in);
Random
  • 467
  • 1
  • 4
  • 9
0

Other answers work great too, I usually go with

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

fscanf reads only till whitespace though, so it's best to use it only if you know the structure of the read file (e.g. fscanf(fp, "%d %d %s", &i1, &i2, string), it seems to me usually more compact.

user3338768
  • 99
  • 1
  • 1
  • 6