Thread: An equivalent of sscanf

  1. #1
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476

    An equivalent of sscanf

    I usually read a config file for my application with a " : " delimiter e.g. :
    Code:
    #name : filename : value : boolstate (0 = false / 1 = true)
    myConf : config.txt :  2000 : 1
    And after I read the whole line into a string, I "tokenize" it with sscanf like so:

    Code:
    ifstream myFile;
    myFile.open("myconfig.cnf");
    
    char buffer[255];
    myFile.getline(buffer, 255);
    
    int res;
    char tempName[255];
    char tempFile[255];
    int value, tempBool;
    res = sscanf ( buffer, "%s : %s : %d : %d", tempName, tempFile, &value, &tempBool);
    std::string name=tempName;
    std::string filename=tempFile;
    bool state=tempBool;
    I understand sscanf is a method from C, so if I wanted to make it more C++, what should I use? Or maybe there's some better way to scan the values from the string that can directly convert the variable type (e.g. from "1000" from the file to int(1000) )? Thanks in advance.
    ERROR: Brain not found. Please insert a new brain!

    “Do nothing which is of no use.” - Miyamoto Musashi.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    have you tried stringstream?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    Quote Originally Posted by vart View Post
    have you tried stringstream?
    Nope. Never use that before. Okay, I'll try that one. Thanks.
    ERROR: Brain not found. Please insert a new brain!

    “Do nothing which is of no use.” - Miyamoto Musashi.

  4. #4
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    Okay I've tried the stringstream. But why can't it convert '0' to 0 but to 48 ? E.g. I have this code:
    Code:
    std::string inputstr('0');
    std::istringstream mystream.str(inputstr);
    unsigned int value;
    mystream>>value; //Value becomes 48, not the intended 0
    Can anybody help me with this? Thanks.

    EDIT: My bad. It turns put to be an unsigned char, not unsigned int. Sorry, didn't check it thoroughly.
    Last edited by g4j31a5; 07-03-2007 at 10:33 PM.
    ERROR: Brain not found. Please insert a new brain!

    “Do nothing which is of no use.” - Miyamoto Musashi.

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    for an nicer way of string2int you can also try
    Code:
    boost::lexical_cast<int>(string_value);

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    '0' is a integer constant
    so probably you want this:
    Code:
    	std::string inputstr("0 15");
    	std::istringstream mystream(inputstr);
    	unsigned int value1, value2;
    	mystream>>value1 >> value2; //Value1 is 0, value 2 is 15
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem using sscanf fgets and overflow checking
    By jou00jou in forum C Programming
    Replies: 5
    Last Post: 02-18-2008, 06:42 AM
  2. Pointer equivalent to array notation
    By bekkilyn in forum C Programming
    Replies: 4
    Last Post: 12-06-2006, 08:22 PM
  3. Problems reading formatted input with sscanf
    By Nazgulled in forum C Programming
    Replies: 17
    Last Post: 05-10-2006, 12:46 AM
  4. Header File Question(s)
    By AQWst in forum C++ Programming
    Replies: 10
    Last Post: 12-23-2004, 11:31 PM
  5. sscanf (I think)
    By RyeDunn in forum C Programming
    Replies: 7
    Last Post: 07-31-2002, 08:46 AM