Click here to Skip to main content
15,993,109 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, how to parse a empty substring with sscanf.......
if my string str[]="is first,,third,final." i cannot able to read the third and final substring if my second substring is zero. Is there an solution to overcome this error.....?????


Code:
C
#include <math.h>
#include <stdio.h>
#include <stdlib.h>

char *tokenstring = "first,second,25.5,15";
int result, i;
double fp;
char o[10], f[10], s[10], t[10];

void main()
{
   result = sscanf(tokenstring,"%[^','],%[^','],%[^','],%s", o,t,s,f);
   fp = atof(s);
   i  = atoi(f);
   printf("%s\n %lf\n %s\n %d\n", o,t,fp,i);
}

for this program if i execute my output is
>first
>second
>25.5
>15


but if i try to remove the substring "second" in the tokenstring pointer... and if i try to execute, then i get output as:

C
char *tokenstring = "first,,25.5,15";


>first
>(blank)
>0.00
>0 




how do i read the third and fourth substring in the string"tokenstring"...any suggestions ??????????
Posted
Updated 9-Feb-12 22:35pm
v2

1 solution

sscanf function (see, for instance here[^]) does NOT take regular expressions in the format string.
You may either:

or

  • Parse yourself the input string (you may wisely use, strtok for the purpose, see strtok documentation[^]).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900