/* sscanf example */
#include <stdio.h>
#include <iostream>
using namespace std;

int main ()
{
 
float f;
int i;
char * the_string = "foo -3.6 fum dum 17";
int r = sscanf(the_string, "foo %f fum dum %d", &f, &i);
cout << f << " " << i << " " << r << endl;
   return 0;  
}

Now what? It's not like you just enlightened everyone with profound insights. This example is trivial and easily obtainable from Google. You aren't answering a direct question and you don't appear to be asking a question either, so why clutter the forum with pointless crap?

i searched google, trying three wrong examples, having compiler errors, this is the tested and working version.

even the website that originally include this example, forgot to put ampersands before variables in the function, next time when people search google using sscanf example, i want them to hit this thread, this is a good intention.

>i searched google, trying three wrong examples, having compiler errors
I searched Google and the first hit was a correct example. But go ahead and do whatever makes you feel special. :icon_rolleyes:

commented: Is it just me or is there a special sexual tension between you two? +18

......

Couldn't have been put in code snippets?

>i searched google, trying three wrong examples, having compiler errors
I searched Google and the first hit was a correct example. But go ahead and do whatever makes you feel special. :icon_rolleyes:

i dont believe you give me the link and your keywords, i will try and see.
this c/c++ thing always makes me feel like i am wasting time to achieve some trivial stuff. i started programming with c#, that is why i feel like that. i am not volunteer to keep c things in my long term memory, that is also why i post such small things so i can reach them easily later.

>give me the link and your keywords, i will try and see.
"sscanf example". That's the first keyword search that came to mind when talking about an sscanf example, and the first hit was this.

>this c/c++ thing always makes me feel like i am wasting time to achieve some trivial stuff.
Wait...really? You're saying that sscanf is less trivial than the equivalent C# code? Perhaps you're that much more of a C# badass than I am, but this is my equivalent solution:

using System;

public class Program {
  static void Main() 
  {
    string[] sep = "foo -3.6 fum dum 17".Split( ' ' );
    float f;
    int i;

    if ( float.TryParse( sep[1], out f )
      && int.TryParse ( sep[4], out i ) )
    {
      Console.WriteLine ( "{0} {1}", f, i );
    }
  }
}

And for comparison, the original code in C++ once again (modified slightly to suit my tastes):

#include <cstdio>
#include <iostream>

int main()
{
  const char *s = "foo -3.6 fum dum 17";
  float f;
  int i;
  
  if ( std::sscanf ( s, "foo %f fum dum %d", &f, &i ) == 2 )
    std::cout<< f <<' '<< i <<'\n';
}

I'd say both examples are pretty darn trivial, but the C# version definitely has more going on.

i respect you julienne :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.