problem with sscanf()

Write here if you have problems with your C source code

Moderator: Board moderators

Post Reply
birdegg
New poster
Posts: 6
Joined: Wed Jan 05, 2005 10:42 pm

problem with sscanf()

Post by birdegg »

Code: Select all

#include<stdio.h>

int main(){
	
	char s[]="2.7183 3.1416 5.142";
	float f;

	while(sscanf(s,"%f",&f)==1){
		printf("%f\n",f);
	}
}
support s[] may contain arbitrary number of floatting points.
how can I pasrse all the floating points with sscanf().
since there are arbitrary number of floatting points, it's impossible
to do with sscanf("%f %f %f...");
but sscanf(s,"%f",&f)==1 always read only the first number,
and cause a infinite loop.
as far as i know the strtok can do this, but how can i do with sscanf?

Lots of thank you for any comments.:D
sumankar
A great helper
Posts: 286
Joined: Tue Mar 25, 2003 8:36 am
Location: calcutta
Contact:

Post by sumankar »

Hi,

The strtok() function takes two arguments: the first one is the string that you want to break up, and next the delimiter string i.e. a string containing all the weird characters based on which you want to break up the first argument.Here it is:

Code: Select all

#include<string.h>
       char*strtok(char*string, constchar*delim);
If string is not NULL, the function scans string for the first occurrence of any character included in delimiters. If it is found, the function overwrites the delimiter in string by a null-character and returns a pointer to the token, i.e. the part of the scanned string previous to the delimiter.
After a first call to strtok, the function may be called with NULL as string parameter, and it will follow by where the last call to strtok found a delimiter.
Note that delimiters may vary from a call to another.

So your problem might be solved as:

Code: Select all

/* strtok example */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main ()
{
  char str[] ="2.7183 3.1416 5.142";
  char * pch;
  double f;

  printf ("Splitting string \"%s\" in tokens:\n",str);
  pch = strtok (str," ");
  while (pch != NULL)
  {
    printf ("%s\n",pch);
    f = atof(pch);
    printf ("%.4lf\n",atof(pch));
    pch = strtok (NULL, " ");
  }
  return 0;
}
Regards,
Suman.

P.S:Do not use

Code: Select all

 char *str ="2.7183 3.1416 5.142";
Strtok will try to alter the contents of what the compiler stores as a read-only string and hence invoke undefined behaviour.
jakabjr
Learning poster
Posts: 56
Joined: Wed Mar 23, 2005 9:21 pm
Location: Timisoara, Romania

Post by jakabjr »

if u really want to use sscanf, u can try this:

Code: Select all

   char s[]="2.7183 3.1416 5.142", *p=s;
   float f;

   while(sscanf(p,"%f",&f)==1){
      printf("%f\n",f);
      p=strchr(p, ' ');
      //since %f consumes all blanks, u don't need the next line
      // while(isspace(*p)) p++;   (#include<ctype.h>)
   }
also, a good function for this is strtof (see also strtod & strtold), like this:

Code: Select all

   char s[]="2.7183 3.1416 5.142", *p=s;
   float f;

   while(*p){
      f = strtof(p,&p);
      printf("%f\n",f);
   } 
in this case i'd rather use strtof than sscanf since it is surely faster. sscanf has some other very usefull properties (like %x").
Understanding a problem in a natural way will lead to a natural solution
sumankar
A great helper
Posts: 286
Joined: Tue Mar 25, 2003 8:36 am
Location: calcutta
Contact:

Post by sumankar »

Code: Select all

%x
is not a property of sscanf(), its a type conversion specifier and its there for the entire scanf/printf family.
Simply nitpicking... :lol:

Suman.
jakabjr
Learning poster
Posts: 56
Joined: Wed Mar 23, 2005 9:21 pm
Location: Timisoara, Romania

Post by jakabjr »

yes, my mistake (or bad language use).
sorry to any viewers.
Understanding a problem in a natural way will lead to a natural solution
Larry
Guru
Posts: 647
Joined: Wed Jun 26, 2002 10:12 pm
Location: Hong Kong and New York City
Contact:

Post by Larry »

using strictly scanf, you can also do something like:

Code: Select all

sscanf( s, "%f %[^\n]", &f, s );
[/code]
Post Reply

Return to “C”