Thread: sscanf vs istringstream

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    330

    sscanf vs istringstream

    What is the istringstream equivalent of sscanf and checking if 4 fields have been read in?

    Code:
    if (sscanf(s, "%d %s %c %s", &id, variable, &type, value) == 4)
    versus

    Code:
    std::istringstream mystream(s);
    
    // scan 4 fields
    mystream >> id;
    mystream >> variable;
    mystream >> type;
    mystream >> value;
    
    if (mystream ????)

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If a stringstream fails to read something, it sets a bad bid, so just check the stream, ie:
    if (mystream) // OK
    if (!mystream) // Not OK

    if (mystream >> id) // OK
    if (!(mystream >> id)) // Not OK
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sscanf error
    By roaan in forum C Programming
    Replies: 6
    Last Post: 08-01-2009, 06:44 AM
  2. istringstream problem in switch block?
    By wolfindark in forum C++ Programming
    Replies: 4
    Last Post: 06-24-2007, 11:56 PM
  3. Replies: 1
    Last Post: 10-27-2006, 01:21 PM
  4. Problems reading formatted input with sscanf
    By Nazgulled in forum C Programming
    Replies: 17
    Last Post: 05-10-2006, 12:46 AM
  5. sscanf (I think)
    By RyeDunn in forum C Programming
    Replies: 7
    Last Post: 07-31-2002, 08:46 AM