Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Strip Whitespace

57 views
Skip to first unread message

Mike Copeland

unread,
Aug 23, 2020, 7:06:07 PM8/23/20
to
I am trying something that I'm not sure is meant to work: using
std::ws with text file i/o. I read a line of text from a file into a
std::string variable. Then I try to assign that string value to an
istringstream object (using std::ws) and from there assign it to another
std::string variable.
My intent is to strip the leading whitespace from each text line.
The code below doesn't work. Please advise. TIA

using namespace std;
string wstr;
istringstream iss;
[read wstr from text file] // new data record
iss >> skipws; // skip leading whitespace
iss.str(wstr);
string str = iss.str(); // leading whitespace NOT stripped



--
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus

Öö Tiib

unread,
Aug 24, 2020, 1:52:26 AM8/24/20
to
On Monday, 24 August 2020 02:06:07 UTC+3, Mike Copeland wrote:
> I am trying something that I'm not sure is meant to work: using
> std::ws with text file i/o. I read a line of text from a file into a
> std::string variable. Then I try to assign that string value to an
> istringstream object (using std::ws) and from there assign it to another
> std::string variable.
> My intent is to strip the leading whitespace from each text line.
> The code below doesn't work. Please advise. TIA
>
> using namespace std;
> string wstr;
> istringstream iss;
> [read wstr from text file] // new data record
> iss >> skipws; // skip leading whitespace
> iss.str(wstr);
> string str = iss.str(); // leading whitespace NOT stripped

That skipws skips whitespace for following >> operators, not for
str().
<http://coliru.stacked-crooked.com/a/1c13e8a37c21fe11>


Vir Campestris

unread,
Aug 24, 2020, 4:56:01 PM8/24/20
to
On 24/08/2020 00:05, Mike Copeland wrote:
> My intent is to strip the leading whitespace from each text line.
> The code below doesn't work. Please advise. TIA

If all you want to do is slip the leading whitespace it would probably
be more efficient to scan for the first non-space character in your read
line, then output the rest of the line. Less in-memory copying.

Andy

Mike Copeland

unread,
Aug 24, 2020, 8:14:12 PM8/24/20
to
That's my intent...but how do I do this? TIA

Siri Cruise

unread,
Aug 24, 2020, 8:43:28 PM8/24/20
to
In article
<MPG.39ae05b3e...@news.eternal-september.org>,
Mike Copeland <mrc...@cox.net> wrote:

> > On 24/08/2020 00:05, Mike Copeland wrote:
> > > My intent is to strip the leading whitespace from each text line.
> > > The code below doesn't work. Please advise. TIA
> >
> > If all you want to do is slip the leading whitespace it would probably
> > be more efficient to scan for the first non-space character in your read
> > line, then output the rest of the line. Less in-memory copying.
> >
> That's my intent...but how do I do this? TIA

C++ is so much better than C.

resetbuffer();
int c = 0; while (c=fgetc(fn), c!=EOF && isspace(c)) ;
while (c!='\n' && c!=EOF) {addbuffer(c); c = fgetc(fn);}
if (emptybuffer() && c==EOF) eofbuffer();

How crude and inefficient.

--
:-<> Siri Seal of Disavowal #000-001. Disavowed. Denied. Deleted. @
'I desire mercy, not sacrifice.' /|\
The first law of discordiamism: The more energy This post / \
to make order is nore energy made into entropy. insults Islam. Mohammed

Sam

unread,
Aug 25, 2020, 7:07:25 AM8/25/20
to
Mike Copeland writes:

> > On 24/08/2020 00:05, Mike Copeland wrote:
> > > My intent is to strip the leading whitespace from each text line.
> > > The code below doesn't work. Please advise. TIA
> >
> > If all you want to do is slip the leading whitespace it would probably
> > be more efficient to scan for the first non-space character in your read
> > line, then output the rest of the line. Less in-memory copying.
> >
> That's my intent...but how do I do this? TIA

line.erase(line.begin(),
std::find_if(line.begin(), line.end(),
[]
(auto c)
{
return c != ' ';
}));

A complete description and explanation of the above C++ library algorithms
and container methods can be found in pretty much every C++ textbook.


Paavo Helde

unread,
Aug 25, 2020, 7:19:30 AM8/25/20
to
This is much more concise and actually strips all leading whitespace,
not just spaces:

line.erase(0, line.find_first_not_of(" \t\r\n"));


Jorgen Grahn

unread,
Aug 25, 2020, 9:45:28 AM8/25/20
to
On Tue, 2020-08-25, Siri Cruise wrote:
> In article
> <MPG.39ae05b3e...@news.eternal-september.org>,
> Mike Copeland <mrc...@cox.net> wrote:
>
>> > On 24/08/2020 00:05, Mike Copeland wrote:
>> > > My intent is to strip the leading whitespace from each text line.
>> > > The code below doesn't work. Please advise. TIA
>> >
>> > If all you want to do is slip the leading whitespace it would probably
>> > be more efficient to scan for the first non-space character in your read
>> > line, then output the rest of the line. Less in-memory copying.
>> >
>> That's my intent...but how do I do this? TIA
>
> C++ is so much better than C.
>
> resetbuffer();
> int c = 0; while (c=fgetc(fn), c!=EOF && isspace(c)) ;
> while (c!='\n' && c!=EOF) {addbuffer(c); c = fgetc(fn);}
> if (emptybuffer() && c==EOF) eofbuffer();
>
> How crude and inefficient.

I'd do it like this:

std::string s;
while(std::getline(is, s)) {
lstrip(s);
do_stuff_with(s);
}

lstrip(std::string&) is trivial to write.

Or if I really didn't want the copying, I'd use std::string_view, or
iterators and implement:

It lstrip(It a, It b);

I don't think I've ever done this though. Normally, once you've
stripped leading whitespace you still have plenty of parsing to do,
and you haven't won a lot by doing one tiny part of it.

It's e.g. more commonly useful to have a split() function which splits
a string on whitespace, while removing said whitespace.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .

Siri Cruise

unread,
Aug 25, 2020, 11:27:53 AM8/25/20
to
In article <slrnrka5f5.1p...@frailea.sa.invalid>,
It's simple deeds should be done cheap.

Mike Copeland

unread,
Aug 25, 2020, 2:41:47 PM8/25/20
to
In article <ri2s3o$ssh$1...@dont-email.me>, ees...@osa.pri.ee says...
Actually, I wound up doing this:

auto iter = str.begin();
while(isspace(*iter)) iter++; // skip over leading whitespace
auto ct = (iter-str.begin());
if(ct > 0) str.erase(0, ct); // erase leading whitespace

which seems to work. Any problems with it?

Paavo Helde

unread,
Aug 25, 2020, 4:33:54 PM8/25/20
to
Yes, it contains a buffer overrun bug if the whole string is whitespace.

Otherwise, it's about the same as my one-liner, except that it trims
also \v and \f, and potentially other characters - isspace() is
locale-dependent and so you never know what it might exactly do on a
customer computer.




0 new messages