This is the mail archive of the libstdc++@sourceware.cygnus.com mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

problem with substr() or string ?


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]