0
/* 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;  
}
3
Contributors
8
Replies
10
Views
8 Years
Discussion Span
Last Post by serkan sendur
Featured Replies
  • 1
    Narue 5,707   8 Years Ago

    >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: Read More

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?

0

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

0

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.

1

>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:

Comments
Is it just me or is there a special sexual tension between you two?
0

>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.

0

>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.

This question has already been answered. Start a new discussion instead.
Take the time to help us to help you. Please be thoughtful and detailed and be sure to adhere to our posting rules.