I'm trying to parse frames formatted in the following scheme :
$[number],[number],[number],<string>;[string]~<string>
Parameters surrounded with '[]' are optional and those surrounded with '<>' are always present defined:
Thus, the following frames are all correct:
$0,0,0,thisIsFirstString;secondString~thirdOne
$0,,0,firstString;~thirdOne
$,,,firstString;~thirdString
Currently I'm able to parse the frame when all elements are present with the following code
int main() {
char frame[100] = "$1,2,3,string1;string2~string3";
char num1[10], num2[10], num3[10], str1[100], str2[100], str3[100];
printf("frame : %s\n", frame);
sscanf(frame,"$%[^,],%[^,],%[^,],%[^;];%[^~]~%s", num1, num2, num3, str1, str2, str3);
printf("Number 1 : %s\n", num1);
printf("Number 2 : %s\n", num2);
printf("Number 3 : %s\n", num3);
printf("String 1 : %s\n", str1);
printf("String 2 : %s\n", str2);
printf("String 3 : %s\n", str3);
return 0;
}
With the following result
frame : $1,2,3,string1;string2~string3
Number 1 : 1
Number 2 : 2
Number 3 : 3
String 1 : string1
String 2 : string2
String 3 : string3
However, if a parameter is missing, the parameters before are well parsed, but not those which are after the missing parameter.
frame : $1,,3,string1;string2~string3
Number 1 : 1
Number 2 :
Number 3 :
String 1 :��/�
String 2 : �\<��
String 3 : $[<��
frame : $1,2,3,string1;~string3
Number 1 : 1
Number 2 : 2
Number 3 : 3
String 1 : string1
String 2 : h�v��
String 3 : ��v��
How can I specify to sscanf
that some parameters are potentially missing in the frame so in that case they will be discarded?