help with sscanf

I need to match a float inside a very long string (about 5000 chars) with sscanf. (I trimmed the string in this example.) I can't seem to match all the chars that come before and after the float.

int main(void)
{
	char A[5000] = "";
	strcat(A, "		hello world! WORD' name='5.3498' hello world!            ");
	float B = 0;

	sscanf(A, "WORD' name='%f", &B); // how can I match all chars before WORD and after the float ?

	printf("%f\n", B);

	return 0;
}

remember: you asked sscanf to match a pattern, and it will ;

what sscanf will not do, is to search (crawl) for a pattern, for this is the purpose of strstr ... see below:

#include <stdio.h>
#include <string.h>
int main(void) {
	char A[5000] = "";
	float B = 0;
	char * begin;

	strcat(A, "		hello world! WORD' name='5.3498' hello world!            ");
	begin = strstr (A, "WORD'") ;
	sscanf(begin, "WORD' name='%f", &B); // how can I match all chars before WORD and after the float ?

	printf("%f\n", B);

	return 0;
}
  • strstr will actually find the beginning of the pattern to match, and sscanf will do what it does best: just scan ...

ok ?

good luck, and success !

alexandre botao

<< botao {dot} org >>