scanf and iostreams

Discussion in 'C++' started by paidojoao-groups@yahoo.com, Jul 30, 2004.

  1. Guest

    Hi,

    How do I get the (s)scanf behaviour below using IOStream?


    #include <iostream>
    #include <cstdio>

    using namespace std;

    int main()
    {
    int m, d, y;

    sscanf("05091968", "%02d%02d%04d", &m, &d, &y);

    cout << m << "/" << d << "/" << y << endl; // 5/9/1968

    return 0;
    }


    Regards,
    Josue Gomes
     
    , Jul 30, 2004
    #1
    1. Advertising

  2. wrote:
    > How do I get the (s)scanf behaviour below using IOStream?
    >
    >
    > #include <iostream>
    > #include <cstdio>
    >
    > using namespace std;
    >
    > int main()
    > {
    > int m, d, y;
    >
    > sscanf("05091968", "%02d%02d%04d", &m, &d, &y);
    >
    > cout << m << "/" << d << "/" << y << endl; // 5/9/1968
    >
    > return 0;
    > }


    Unfortunately, you cannot. The simplest work-around is to extract
    the characters, put them in a string, then use 'sscanf', just like
    you do here.

    Victor
     
    Victor Bazarov, Jul 30, 2004
    #2
    1. Advertising

  3. marbac Guest

    wrote:
    > Hi,
    >
    > How do I get the (s)scanf behaviour below using IOStream?
    >


    Hi,

    I dont think that this is possible with iostreams directly.
    But you also can use atoi and the c++ string class to solve this problem

    regards marbac


    #include <iostream>
    #include <string>
    #include <cstdlib>
    using namespace std;

    int main()
    {
    string test="05091968";
    cout << atoi(test.substr(0,2).c_str()) << "/"
    << atoi(test.substr(2,2).c_str()) << "/"
    << atoi(test.substr(4,4).c_str()) << endl; // 5/9/1968
    return 0;
    }

    or without using c-style headers:

    #include <iostream>
    #include <sstream>
    #include <string>
    #include <cstdlib>

    using namespace std;

    int str2int(string str) {
    istringstream istr (str);
    int i;
    istr >> i;
    return i;
    }

    int main()
    {
    string test="05091968";
    cout << str2int(test.substr(0,2)) << "/"
    << str2int(test.substr(2,2)) << "/"
    << str2int(test.substr(4,4)) << endl; // 5/9/1968
    return 0;
    }
     
    marbac, Jul 30, 2004
    #3
  4. marbac Guest

    wrote:
    > Hi,
    >
    > How do I get the (s)scanf behaviour below using IOStream?
    >
    >

    Hi,

    I dont think that this is possible with iostreams directly.
    But you also can use atoi and the c++ string class to solve this problem

    regards marbac


    #include <iostream>
    #include <string>
    #include <cstdlib>
    using namespace std;

    int main()
    {
    string test="05091968";
    cout << atoi(test.substr(0,2).c_str()) << "/"
    << atoi(test.substr(2,2).c_str()) << "/"
    << atoi(test.substr(4,4).c_str()) << endl; // 5/9/1968
    return 0;
    }

    or without using c-style headers:

    #include <iostream>
    #include <sstream>
    #include <string>


    using namespace std;

    int str2int(string str) {
    istringstream istr (str);
    int i;
    istr >> i;
    return i;
    }

    int main()
    {
    string test="05091968";
    cout << str2int(test.substr(0,2)) << "/"
    << str2int(test.substr(2,2)) << "/"
    << str2int(test.substr(4,4)) << endl; // 5/9/1968
    return 0;
    }
     
    marbac, Jul 30, 2004
    #4
  5. marbac Guest

    marbac wrote:
    > wrote:
    >
    >> Hi,
    >>
    >> How do I get the (s)scanf behaviour below using IOStream?
    >>
    >>

    > Hi,
    >
    > I dont think that this is possible with iostreams directly.
    > But you also can use atoi and the c++ string class to solve this problem
    >
    > regards marbac


    But then i think the sscanf method and usage of char* is faster
     
    marbac, Jul 30, 2004
    #5
  6. In message <rzyOc.50741$>, marbac
    <> writes
    >marbac wrote:
    >> wrote:
    >>
    >>> Hi,
    >>>
    >>> How do I get the (s)scanf behaviour below using IOStream?
    >>>
    >>>

    >> Hi,
    >> I dont think that this is possible with iostreams directly.
    >> But you also can use atoi and the c++ string class to solve this problem
    >> regards marbac

    >
    >But then i think the sscanf method and usage of char* is faster


    But is speed actually a problem? ("premature optimisation considered
    evil") If you have reason to think so, find out objectively whether it
    really is; if not, do whatever is clearest.

    --
    Richard Herring
     
    Richard Herring, Aug 4, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mahadeva
    Replies:
    2
    Views:
    393
  2. Mahadeva
    Replies:
    0
    Views:
    352
    Mahadeva
    Oct 12, 2004
  3. =?ISO-8859-1?Q?Martin_J=F8rgensen?=

    scanf (yes/no) - doesn't work + deprecation errors scanf, fopen etc.

    =?ISO-8859-1?Q?Martin_J=F8rgensen?=, Feb 16, 2006, in forum: C Programming
    Replies:
    185
    Views:
    3,517
    those who know me have no need of my name
    Apr 3, 2006
  4. =?ISO-8859-1?Q?Martin_J=F8rgensen?=

    difference between scanf("%i") and scanf("%d") ??? perhaps bug inVS2005?

    =?ISO-8859-1?Q?Martin_J=F8rgensen?=, Apr 26, 2006, in forum: C Programming
    Replies:
    18
    Views:
    703
    Richard Bos
    May 2, 2006
  5. Imre Palik

    C++ and streams (not iostreams)

    Imre Palik, Mar 13, 2006, in forum: C++
    Replies:
    0
    Views:
    337
    Imre Palik
    Mar 13, 2006
Loading...

Share This Page