i have a string "success=1&u=0&name=bint&u=1&name=lucy&u=2&name=barry" etc
i can use sscanf(string,"success=%d", &d) to get the success value. but
after that i just want to read name and u pairs until there are no more. if
Iwere to do a
sscanf (string,"success=%d&u=%d&name=%s"), that would get me the values of
the first u/name, right? is there any way to retrieve the string pointer
position from sscanf so that I can just call it again from that point in the
string?
You've gotten a couple of suggestions already, but I'll throw another
into the mix, just for fun:
// warning: only minimally tested. Makes no attempt at verifying or
// reacting reasonably to bad input.
#include <sstream>
#include <algorithm>
#include <map>
#include <iostream>
#include <string>
#include <stdlib.h>
// the real guts: read a single 'name=value' pair. Written as a template
// so the value can be an int, string, or anything else we can extract
// from a stream.
template<class T>
std::istream &getvalue(std::istream &is, T &value) {
// first read the whole 'name=value' pair
std::string temp;
std::getline(is, temp, '&');
// then find the value part:
int pos = temp.find('=');
std::string temp2(temp.substr(pos+1,-1));
// and read the value:
std::stringstream t(temp2);
t >> value;
return is;
}
typedef std:
air<int, std::string> uname;
// not technically allowed, but won't be found unless in std namespace:
namespace std {
std::istream &operator>>(std::istream &is, uname &un) {
getvalue(is, un.first);
getvalue(is, un.second);
return is;
}
std:
stream &operator<<(std:
stream &os, uname const &un) {
return os << un.first << ":\t" << un.second;
}
}
int main() {
std::stringstream input("success=1&u=0&name=bint"
"&u=1&name=lucy&u=2&name=barry");
std::map<int, std::string> values;
int success;
getvalue(input, success);
// read in the data
std::copy(std::istream_iterator<uname>(input),
std::istream_iterator<uname>(),
std::inserter(values, values.begin()));
// and display what we read:
std::copy(values.begin(), values.end(),
std:
stream_iterator<uname>(std::cout, "\n"));
return 0;
}