By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,171 Members | 1,020 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,171 IT Pros & Developers. It's quick & easy.

Setting mininum string read width in istream

P: n/a
It seems odd that there is (apparently) no way of telling the istream
operator overloading for strings to NOT stop when it gets to the

first space.

What I want to do is this:

string source("7 one two 5 fruit 3 tea 6 banana");
istringstream iss(source);
int strLen;
iss >> strLen; // get length of next string, 7 in first case
iss.force_next_string_read_to_be_this_length(strLe n);
string s;
iss >> s; // s = "one two" in first case

Any elegant solutions? Don't think so - iss.width() is all about
maximum lengths, not minimum.
Jul 23 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

Richard Clay wrote:
It seems odd that there is (apparently) no way of telling the istream
operator overloading for strings to NOT stop when it gets to the

first space.

What I want to do is this:

string source("7 one two 5 fruit 3 tea 6 banana");
istringstream iss(source);
int strLen;
iss >> strLen; // get length of next string, 7 in first case
iss.force_next_string_read_to_be_this_length(strLe n);
string s;
iss >> s; // s = "one two" in first case


Slighly ugly, but how about:

vector<char> buf( strLen+1, '\0' );
size_t realLen = buf.get( &*s.begin(), s.size() );
string s( buf.begin(), buf.begin() + realLen );

--
Richard Smith

Jul 23 '05 #2

P: n/a
ri*****@ex-parrot.com schrieb:
Richard Clay wrote:
It seems odd that there is (apparently) no way of telling the istream
operator overloading for strings to NOT stop when it gets to the


first space.

What I want to do is this:

string source("7 one two 5 fruit 3 tea 6 banana");
istringstream iss(source);
int strLen;
iss >> strLen; // get length of next string, 7 in first case
iss.force_next_string_read_to_be_this_length(str Len);
string s;
iss >> s; // s = "one two" in first case

Slighly ugly, but how about:

vector<char> buf( strLen+1, '\0' );
size_t realLen = buf.get( &*s.begin(), s.size() );
string s( buf.begin(), buf.begin() + realLen );

Perhaps slightly less ugly:
string s( strLen, 0 );
size_t realLen = buf.get( &s[ 0 ], s.size() );
s.resize( realLen );

Cheers,
Malte
Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.