C++: stripping '/' char from istream

wxjunkie

Senior member
Nov 6, 2000
409
0
0
In C++... What's the easiest way to strip a "slash" out of an input stream so I can just work with what's left?
 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
I guess you mean pull them all out and replace with nothing?

So you take /usr/blah/blah and convert it to usrblahblah before working with it?
 

Adrian Tung

Golden Member
Oct 10, 1999
1,370
1
0
How about simply initializing a temporary buffer, then use a for loop to go through each character in the stream. If not slash character, put it in temp buffer and increment buffer length, else ignore. At the end, null-terminate the buffer.

Crude, but works.


:)atwl
 

EmperorRob

Senior member
Mar 12, 2001
968
0
0
Depends on what kind of string you are using. The old string or the STL string.

Once you learn the STL strings they are a lot more flexible and easier to do stuff like this with.

#include <string>
using namespace std;

string dir = "/usr/local/bin";
for (string::iterator s = dir.begin(); s != dir.end(); ) {
if (s == '/')
s.erase();
else
++s;
}


I'm about 95% sure that's the way I did it. My source is at the office so if that doesn't work for you I'll doublecheck.

EDIT: Sorry I thought you said string instead of stream. ;)