I receive some data every 10 seconds from an external machine (always 4 lines) like:
Yesterday match:
Player_1:(P=31,Reb=12)
Yesterday match:
Player_2:(P=12,Reb=2)
I have the following code which reads the data (I have also open and configure serial ports functions):
int learn_data(int fd)
{
int n,i;
char buff[200];
memset(buff, 0, sizeof(buff));
char* ptr;
FILE *fp=fdopen(fd,"r");
while(fgets(buff, sizeof(buff), fp) != NULL)
{
printf("%s", buff);
}
}
With this code fgets reads line by line the buffer. Now my question is, how can I get The values of points and rebounds (31,12), (12,2) etc with the usage of strtok? How can I escape the first and third line?
If you have a well-formatted input, you're okay with sscanf()
. Try the following:
int P, Reb;
sscanf(buff, "%*7s%*d%*4s%d%*5s%d", &P, &Reb);
To deal only with Player*
, you can do memcmp()
first. Like,
if(memcmp(buff, "Player_", 7) == 0) ...