This is the mail archive of the
libstdc++@sourceware.cygnus.com
mailing list for the libstdc++ project.
problem with substr() or string ?
- To: libstdc++ at sourceware dot cygnus dot com
- Subject: problem with substr() or string ?
- From: Dawit Yimam <Dawit dot Yimam at gmd dot de>
- Date: Thu, 03 Feb 2000 16:06:53 +0100
- Newsgroups: gnu.gcc.help
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