i have a string "success=1&u=0& name=bint&u=1&n ame=lucy&u=2&na me=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,"succes s=%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 9 6649
On Mon, 14 Jan 2008 18:06:12 +0100, Bint <bi**@csgs.comw rote:
i have a string "success=1&u=0& name=bint&u=1&n ame=lucy&u=2&na me=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,"succes s=%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.
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*******@wana doo.frwrote in message
news:op.t4xkrqj urttu86@debian. ..
On Mon, 14 Jan 2008 18:06:12 +0100, Bint <bi**@csgs.comw rote:
i have a string "success=1&u=0& name=bint&u=1&n ame=lucy&u=2&na me=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,"succes s=%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.
On Mon, 14 Jan 2008 18:50:20 +0100, Bint <bi**@csgs.comw rote:
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*******@wana doo.frwrote in message
news:op.t4xkrqj urttu86@debian. ..
On Mon, 14 Jan 2008 18:06:12 +0100, Bint <bi**@csgs.comw rote:
>i have a string "success=1&u=0& name=bint&u=1&n ame=lucy&u=2&na me=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,"succes s=%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.
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
On Mon, 14 Jan 2008 19:00:33 +0100, Bint <bi**@csgs.comw rote:
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* ....
On Jan 14, 6:41 pm, David Côme <davidc...@wana doo.frwrote:
On Mon, 14 Jan 2008 18:06:12 +0100, Bint <b...@csgs.comw rote:
i have a string "success=1&u=0& name=bint&u=1&n ame=lucy&u=2&na me=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,"succes s=%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 objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
"Bint" <bi**@csgs.comw rote:
i have a string "success=1&u=0& name=bint&u=1&n ame=lucy&u=2&na me=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,"succes s=%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_iterato r<char>( is ), istream_iterato r<char>(), c );
return is;
}
int main()
{
const char* str =
"success=1&u=0& name=bint&u=1&n ame=lucy&u=2&na me=barry";
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';
}
}
On Jan 14, 3:39 pm, "Daniel T." <danie...@earth link.netwrote:
"Bint" <b...@csgs.comw rote:
i have a string "success=1&u=0& name=bint&u=1&n ame=lucy&u=2&na me=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,"succes s=%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_iterato r<char>( is ), istream_iterato r<char>(), c );
return is;
}
int main()
{
const char* str =
"success=1&u=0& name=bint&u=1&n ame=lucy&u=2&na me=barry";
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.
In article <13************ *@corp.supernew s.com>, bi**@csgs.com says...
i have a string "success=1&u=0& name=bint&u=1&n ame=lucy&u=2&na me=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,"succes s=%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.subs tr(pos+1,-1));
// and read the value:
std::stringstre am t(temp2);
t >value;
return is;
}
typedef std::pair<int, std::stringunam e;
// 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::stringstre am input("success= 1&u=0&name=bint "
"&u=1&name=lucy &u=2&name=barry ");
std::map<int, std::stringvalu es;
int success;
getvalue(input, success);
// read in the data
std::copy(std:: istream_iterato r<uname>(input) ,
std::istream_it erator<uname>() ,
std::inserter(v alues, values.begin()) );
// and display what we read:
std::copy(value s.begin(), values.end(),
std::ostream_it erator<uname>(s td::cout, "\n"));
return 0;
}
--
Later,
Jerry.
The universe is a figment of its own imagination. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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
|
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
> for the %s format (for example, "32%s" instead of "%s"); otherwise,
> improperly formatted input can easily cause a buffer overrun.
If a programmer obeys MSDN and specifies a format like "32%s" then
improperly formatted input can easily cause a...
|
by: Ithaqua |
last post by:
How do I do this?
--
Cheers
Ithaqua
|
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);
....
| |
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 go about constructing the character reader in VBA to assign each string to an array element. I'm assuming that I need to read the row source and read each string up to a semicolon then repeat?
Any Help would be great as I am bamboozled thanks!
|
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 %2u:%2u:%2u" which works, but it
allows each numeric field to be either 1 or 2 digits in length. Also the man
page for sscanf says that preceeding white space before a numeric is
ignored.
|
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 need to check the presence of these 4 fields and also the 3 tabs separating the fields.
Sample line: + 12345678 23.22 Initial
My format string for sscanf is
|
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 the line and then use something like
sscanf(buf0, "%s %ld %ld %s %s %s %s %s", chr, &bg, &ed, zg, var, ref, s1, s2);
The program will skip these empty field and assign the value of the next non-empty field to the variable. What I really...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |