dali1985
dali1985

Reputation: 3313

Return values with sscanf

I would like to ask you how can I get the values 043, 21.45, 45.65, 9.34, 3.46 with the usage of sscanf.

Parameters: 43 VALUES FROM 0000:0000 (043)
Name_8:(Temp=21.45,Hum=45.65,AR=9.34,Volt=3.46V)

Until the FROM I know that the code is something like that

char str[20];
sscanf(buff,"%*s %*d %*s %s",str);
printf("Results %s\n",str);

And this returns me

Results FROM

But I do not know how to continue with sscanf. I am not very familiar and I do not know how to escape characters like (),_,: etc.

Upvotes: 3

Views: 8257

Answers (3)

Jens
Jens

Reputation: 72639

The parts of the input that don't change can be specified literally, you don't need %*s to skip them. The variant part can be skipped with the %*d and %*s you use. Running this

#include <stdio.h>

int main (void)
{
    char buff[] = "Parameters: 43 VALUES FROM 0000:0000 (043)\n"
                  "Name_8:(Temp=21.45,Hum=45.65,AR=9.34,Volt=3.46V)";
    int tag;
    float Temp, Hum, AR, Volt;
    int n;

    n = sscanf (buff, "Parameters: %*d VALUES FROM %*d:%*d (%d) "
                      "Name_%*d:(Temp=%f,Hum=%f,AR=%f,Volt=%fV)",
                       &tag,    &Temp,   &Hum, &AR,  &Volt);
    if (n == 5) {
        printf ("tag = %d, Temp = %f, Hum = %f, AR = %f, Volt = %f\n",
                 tag, Temp, Hum, AR, Volt);
    } else {
        printf ("Couldn't scan all items (converted %d).\n", n);
    }
    return 0;
}

will output

tag = 43, Temp = 21.450001, Hum = 45.650002, AR = 9.340000, Volt = 3.460000

Always check the return value of sscanf! You don't want to soldier on with garbage values. And note that a single space in the format skips any amount of whitespace, so the space after ) skips the newline in the input.

Upvotes: 5

BLUEPIXY
BLUEPIXY

Reputation: 40145

#include <stdio.h>

int main(void){
    char buff[] = "Parameters: 43 VALUES FROM 0000:0000 (043)\n"
                "Name_8:(Temp=21.45,Hum=45.65,AR=9.34,Volt=3.46V)";
    char dev[25], temp[25], hum[25], ar[25], volt[25];
    int pos = 0, len;
    sscanf(buff, "%*[^(](%[^)])%n", dev, &len);
    pos += len;
    sscanf(buff + pos, "%*[^(](Temp=%[^,],%n", temp, &len);
    pos += len;
    sscanf(buff + pos, "Hum=%[^,],%n", hum, &len);
    pos += len;
    sscanf(buff + pos, "AR=%[^,],%n", ar, &len);
    pos += len;
    sscanf(buff + pos, "Volt=%[^V]V%n", volt, &len);
    printf("dev:%s\n", dev);
    printf("temp:%s\n", temp);
    printf("hum:%s\n", hum);
    printf("ar:%s\n", ar);
    printf("volt:%s\n", volt);
    return 0;
}

Upvotes: 0

praks411
praks411

Reputation: 1992

For first line you can try something like this.But second line could be tricky :)

 char f1[] = "Parameters: 43 VALUES FROM 0000:0000 (043)";
 int d = 0;
 sscanf(f1, "%*s %*s %*s %*s %*s (%d)",&d);
 printf("D %d \n", d);

However if you can modify the second like by adding space between numeric values

 char f2[] = "Name_8:(Temp= 21.45,Hum= 45.65,AR= 9.34,Volt= 3.46V)";

Then you can use this to read float values

float te= 0, h = 0, ar = 0, v = 0;
sscanf(f2, "%*s %f%*s %f%*s %f%*s %f)",&te, &h, &ar, &v);

Upvotes: 0

Related Questions