what is the C++ version of this C code ?

This is a discussion on what is the C++ version of this C code ? within the C++ Programming forums, part of the General Programming Boards category; hi, i want to extract data from a line. something like.... char buff[100]; in.getline(buff,100); // in is an inputstream object ...

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    92

    Exclamation what is the C++ version of this C code ?

    hi, i want to extract data from a line.

    something like....

    char buff[100];
    in.getline(buff,100); // in is an inputstream object
    if(sscanf(buff,"%d %d",&var1,&var2)==2) // what is the C++
    //version of this line ?
    cout<<var1<<" "<<var2<<endl;


    there are several ways to read data...but i want to know, what is the equivalent code for sscanf() in C++ ? is it possible at all ?
    blue_gene

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,973
    you can use sscanf() in C++, but you'll need to #include <cstdio>
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Or you can look up the stringstream class.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  4. #4
    Registered User
    Join Date
    Dec 2003
    Posts
    92
    you can use sscanf() in C++, but you'll need to #include <cstdio>
    ....yes,as C++ is a superset of C. but that will not be pure C++ programming....it will be mixed thing(although no error).


    actually, i was thinking a C code

    fgets(buff,100,ptr) // it is similar to getline() in C++
    sscanf( buff,"%d %d",&var1,&var2) // what is similar in C++?

    in fact in this case i was expecting similar C++ function if exist .

    thanks
    blue_gene

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >but that will not be pure C++ programming....
    Yes it will. If the C++ standard defines a construct, then it's "pure" C++. fgets and sscanf just don't conform to your idea of "pure" C++.

    >it will be mixed thing(although no error).
    And what's wrong with that? It seems to me that you're needlessly restricting yourself with this skewed concept of "pure" C++.

    >in fact in this case i was expecting similar C++ function if exist
    Pure C++ doesn't use archaic things like functions, it uses objects. If you want a one-liner, sscanf is there. Otherwise you're stuck with stringstreams:
    Code:
    string buff;
    int var1, var2;
    //fgets(buff,100,ptr) // it is similar to getline() in C++
    getline ( cin, buff );
    //sscanf( buff,"%d %d",&var1,&var2) // what is similar in C++?
    istringstream cppsscanf ( buff );
    cppsscanf>> var1 >> var2;
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    Just to clarify a bit, the equivilant of sscanf(...) == 2 is to evaluate the istringstream in a boolean context, or test !fail(). Here is preludes code with a bit more error checking.
    Code:
    std::string buff;
    if(std::getline(std::cin, buff)) {
        std::istringstream iss(buff);
        int a,b;
        if(iss >> a >> b) {
            std::cout << a << ", " << b << std::endl;
        } else {
            std::cerr << '"' << iss.str() << "\" does not start with two ints\n";
            iss.clear(); // clear all errors
            char ch;
            if(iss.get(ch)) {
                std::cerr << "confused by '" << ch << "' at pos " 
                          << static_cast<int>(iss.tellg())-1 << std::endl;
            } else if(iss.eof()) {
                std::cerr << "unexpected end of file\n";
            } else { // in theory we never reach here
                std::cerr << "unrecoverable internal stream error\n";
            }
        }
    } else {
        std::cerr << "Could not read from standard input\n";
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to set File Version of VC++ 6 dll
    By mercury529 in forum Windows Programming
    Replies: 3
    Last Post: 12-08-2006, 01:49 PM
  2. Writing Code
    By ILoveVectors in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2005, 12:27 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21