0

Hi i have a function like this

while(fscanf(fp,"\n%d\t%s\t%s\t%X%X\t%d\t  \n",&record.Index,record.Name,record.Empcode,&record.CSN_MSB,&record.AccessRights)!=EOF)
 {
  printf("\nIndex: %d\nEmployee Name: %s\nEmpcode: %s\nCSN: %X\nAccessRights: %d\n",record.Index,record.Name,record.Empcode,record.CSN_MSB,record.AccessRights);
  sprintf(CSN_MSB_LSB,"%X", record.CSN_MSB);
  if(strncmp(CSN_MSB_LSB,str,8)==0)
  found=1;       
 }

in this code my fscanf is reading only one line from file pointer fd, i want to read all the lines from the file. how i can i do this with same fscanf function or else any alternative which contains the same parameter list for the fscanf function please suggest me

amar
  • 509
  • 3
  • 8
  • 17

1 Answers1

0

I would try something of the sort:

    while(fscanf(fp,"%d%s%s%X%X%[^\n]*c",
          &record.Index,record.Name,record.Empcode,
          &record.CSN_MSB,&record.AccessRights)!=EOF)
    {

Though, it is worth noting that you are scanning 6 items and only storing 5. Also, you are using sscanf which takes a pointer to a character and passing it a file pointer (file descriptor), you want to use fscanf if reading from a file. The last number you scan never gets stored. The "[^\n]" says scan until a newline and takes place of the last number you are scanning for (though you don't save it in your example) and the "*c" consumes that newline. See this.

Community
  • 1
  • 1
blrg891
  • 52
  • 2