This is the mail archive of the
libstdc++@sourceware.cygnus.com
mailing list for the libstdc++ project.
RE: problem with substr() or string ?
- To: "Dawit Yimam" <Dawit dot Yimam at gmd dot de>, <libstdc++ at sourceware dot cygnus dot com>
- Subject: RE: problem with substr() or string ?
- From: "Matthew Brown" <mbrown at mediadb dot net>
- Date: Thu, 3 Feb 2000 10:28:50 -0500
It looks like the file might have CRLF line termination so that you are getting 'R\r' in "field2".
-- Matthew Brown
------------ Original Message --------------
Hi All,
Please help me with this:
I have the following simple code to read from file a line at a tine and
identify substrings:
#include <string>
#include <fstream>
using namespace std;
int main ()
{
string line;
unsigned int separator;
string field1;
string field2;
ifstream testf ("testfile");
while (getline (testf, line)) {
separator = line.find('|');
if (separator != string::npos) {
field1 = line.substr (0, separator);
field2 = line.substr (separator + 1);
cout << field2.size() << endl;
}
}
return (0);
}
And in the file "testfile", I have something like the following:
xyz|R
zzzzz|K
ytz234|M
My problem is that what I get from the field2.size() is 2 rather than 1
except the last line which is not terminated by line return. Why is this
? I even couldn't match the field2 values with conditions like:
if (field2 == "R") { .... }
Thanks.
Dawit