473,385 Members | 1,796 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

string reading with sscanf

i have a string "success=1&u=0&name=bint&u=1&name=lucy&u=2&name=ba rry" 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?

thanks
B
Jan 14 '08 #1
9 6625
On Mon, 14 Jan 2008 18:06:12 +0100, Bint <bi**@csgs.comwrote:
i have a string "success=1&u=0&name=bint&u=1&name=lucy&u=2&name=ba rry"
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 thevalues
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?

thanks
B

use ostringstream and istringstream instead of sscanf.

Jan 14 '08 #2
I don't think my compiler has that. Is that part of standard C/C++? Is
there an include file for it?

Thanks
B
"David Côme" <da*******@wanadoo.frwrote in message
news:op.t4xkrqjurttu86@debian...
On Mon, 14 Jan 2008 18:06:12 +0100, Bint <bi**@csgs.comwrote:
i have a string "success=1&u=0&name=bint&u=1&name=lucy&u=2&name=ba rry"
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?

thanks
B

use ostringstream and istringstream instead of sscanf.
Jan 14 '08 #3
On Mon, 14 Jan 2008 18:50:20 +0100, Bint <bi**@csgs.comwrote:
I don't think my compiler has that. Is that part of standard C/C++? Is
there an include file for it?

Thanks
B
"David Côme" <da*******@wanadoo.frwrote in message
news:op.t4xkrqjurttu86@debian...
On Mon, 14 Jan 2008 18:06:12 +0100, Bint <bi**@csgs.comwrote:
>i have a string "success=1&u=0&name=bint&u=1&name=lucy&u=2&name=ba rry"
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?

thanks
B

use ostringstream and istringstream instead of sscanf.

ostringstream and istringstream are standard classes of C++
PS: Don't reply at top.
Jan 14 '08 #4
ok thank you I got it. it's not clear right off how to use that do what I'm
trying to do though, i haven't really used streams at all
Jan 14 '08 #5
On Mon, 14 Jan 2008 19:00:33 +0100, Bint <bi**@csgs.comwrote:
ok thank you I got it. it's not clear right off how to use that do what
I'm
trying to do though, i haven't really used streams at all


Generaly use C++ tools when you can and C tools when you must.
So use std::string instead of array of char,stream instead of FILE* ....
Jan 14 '08 #6
On Jan 14, 6:41 pm, David Côme <davidc...@wanadoo.frwrote:
On Mon, 14 Jan 2008 18:06:12 +0100, Bint <b...@csgs.comwrote:
i have a string "success=1&u=0&name=bint&u=1&name=lucy&u=2&name=ba rry"
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?
use ostringstream and istringstream instead of sscanf.
I'm not sure that either are really appropriate here. If I've
understood him correctly, he's got a '&' separated list of
attribute value pairs. The easiest way of handling this is
probably something like my FieldArray classes; a first pass
which converts the string into a vector of strings, with one
attribute value pair in each element. Then split up the
elements into the attribute and the value (perhaps
std::transform with a conversion function---boost::regex could
probably help here).

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jan 14 '08 #7
"Bint" <bi**@csgs.comwrote:
i have a string "success=1&u=0&name=bint&u=1&name=lucy&u=2&name=ba rry" 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?
I would do something like this:
#include <algorithm>
#include <iostream>
#include <iterator>
#include <sstream>
#include <string>

using namespace std;

istream& find( istream& is, char c )
{
find( istream_iterator<char>( is ), istream_iterator<char>(), c );
return is;
}

int main()
{
const char* str =
"success=1&u=0&name=bint&u=1&name=lucy&u=2&name=ba rry";

stringstream ss( str );
find( ss, '=' );
int i = 0;
ss >i;
cout << "success = " << i << '\n';
int u;
string name;
while ( find( ss, '=' ) && ss >u &&
find( ss, '=' ) && getline( ss, name, '&' ) )
{
cout << "u = " << u << " name = " << name << '\n';
}
}
Jan 14 '08 #8
On Jan 14, 3:39 pm, "Daniel T." <danie...@earthlink.netwrote:
"Bint" <b...@csgs.comwrote:
i have a string "success=1&u=0&name=bint&u=1&name=lucy&u=2&name=ba rry" 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?

I would do something like this:

#include <algorithm>
#include <iostream>
#include <iterator>
#include <sstream>
#include <string>

using namespace std;

istream& find( istream& is, char c )
{
find( istream_iterator<char>( is ), istream_iterator<char>(), c );
return is;

}

int main()
{
const char* str =
"success=1&u=0&name=bint&u=1&name=lucy&u=2&name=ba rry";

stringstream ss( str );
find( ss, '=' );
int i = 0;
ss >i;
cout << "success = " << i << '\n';
int u;
string name;
while ( find( ss, '=' ) && ss >u &&
find( ss, '=' ) && getline( ss, name, '&' ) )
{
cout << "u = " << u << " name = " << name << '\n';
}

}
I would use a tokenizer to do that. You can use the boost tokenizer
for that giving the token & or you can create your own by reading each
character and comparing it with '&' to separate the whole string in
tokens.
Jan 14 '08 #9
In article <13*************@corp.supernews.com>, bi**@csgs.com says...
i have a string "success=1&u=0&name=bint&u=1&name=lucy&u=2&name=ba rry" 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::pair<int, std::stringuname;

// 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::ostream &operator<<(std::ostream &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::stringvalues;

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::ostream_iterator<uname>(std::cout, "\n"));
return 0;
}

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jan 15 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Steve | last post by:
Hi, How can I convert a C++ string to an integer and then an integer back to a string? Is there built-in support for this? Thanks, Steve
4
by: smshahriar | last post by:
Hi, I want to scan from the following string all the hex numbers and populate an array of integers: 0x27 0x00 0x30 0x00 0x33 0x00 0x36 0x00
1
by: Norman Diamond | last post by:
Page http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt_sscanf.2c_.swscanf.asp says: > Security Note When reading a string with sscanf, always specify a width >...
10
by: Ithaqua | last post by:
How do I do this? -- Cheers Ithaqua
7
by: gio | last post by:
suppose I have: .... char str1; char str2; int ret; fgets(str1, LEN, stdin); //str1 can contain just '\n' and '\0' ret=sscanf(str1, "%s", str2); ....
3
by: gav82 | last post by:
Hi, I'm new to Access and was wondering if someone could help me with a little problem. If I am trying to get strings from an unbound listbox on a form and assign them to an array. How do I...
6
by: Richard | last post by:
I'm validating a date and time string which must be EXACTLY of the format yy-mm-dd hh:mm:ss and extracting the six numeric values using sscanf. I'm using the format string "%2u-%2u-%2u...
1
by: agarwalsuchin | last post by:
I am reading a line and then using sscanf to split it into individual fields. But, somehow sscanf is reading the first character as null. I have four fields in my line separated by tabs. I...
1
by: Shan Yang | last post by:
Hi, I am handling tab delimited txt files that have multiple fields. But some of the fields can be empty. So it will appear as <TAB><TAB> in the file rather than <TAB>something<TAB>. If I read in...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.