0

I want to parse from a file that has the following format (it may change) the number that is after t= (e.g 19625):

3a 01 4b 46 7f ff 06 10 42 : crc=42 YES\n
3a 01 4b 46 7f ff 06 10 42 t=19625
int t;
fp=fopen("text","r");
fscanf(fp,"t=%d",&t);
fclose(fp);
printf("%d\n",t);

does not give the output.. Any suggestions?

Jongware
  • 22,200
  • 8
  • 54
  • 100
user2030431
  • 761
  • 1
  • 6
  • 8

3 Answers3

1

modify like this

    int t;
    char buff[32];
    FILE *fp=fopen("text","r");
    while(EOF!=fscanf(fp, "%s", buff)){
        if(1==sscanf(buff, "t=%d",&t)){
            break;
        }
    }
    fclose(fp);
    printf("%d\n",t);
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
0

Here, the right keyword is "hexadecimal"

see How do I read hex numbers into an unsigned int in C

see http://www.cplusplus.com/reference/cstdio/fscanf/

Try

  int readt[4];
  fscanf(fp,"t=%x %x %x %x",&readt[0],&readt[1],&readt[2],&readt[3]);
  t=((256*readt[0]+readt[1])*256+readt[2])*256+readt[3];
  printf("t=%d\n",t);

Bye,

Francis

Community
  • 1
  • 1
francis
  • 9,525
  • 2
  • 25
  • 41
0

As long as the file doesn't contain a t before the t=, you could simply replace the

fscanf(fp,"t=%d",&t);

with

fscanf(fp, "%*[^t]t=%d", &t);
Armali
  • 18,255
  • 14
  • 57
  • 171