Friday, 15 February 2013

c++ - string from istringstream getline not comparing right -



c++ - string from istringstream getline not comparing right -

i reading text file, comparing first line of file, "abc", string "abc" , not evaluating equal, although log statement showing same. (it's outputting abc!=abc)

what noticed is, if create text file 1 line evaluates equal.

i've tried things such using strcmp or compare function same results. i've tried making string comparing "abc\n" , didn't work either.

here code:

string teststring = fileutils::getinstance()->getstringfromfile("test.txt"); istringstream ss(teststring); string s; string s2 = "abc"; getline(ss, s, '\n'); if(s == s2){ cclog("%s","we good..."); } else{ cclog("%s!=%s", s.c_str(), s2.c_str()); }

note strings contain "invisible" characters. notably, systems represent end of lines using line end sequence, e.g., "\r\n" sequence. if case, string may same contain carriage return. can check printing string so:

std::copy(s.begin(), s.end(), std::ostream_iterator<int>(std::cout, " "));

if there carriage homecoming you'd like

97 98 99 13

the confusing aspect said output see is

abc=abc

i expect be

=abc

as output be

abc\r=abc

and '\r' should reposition cursor @ start of line. unless cclog() ends removing carriage homecoming characters.

c++ string fstream

No comments:

Post a Comment