Reputation: 720
i have a little problem with the following code:
std::string compare = "data_END";
while ((!(temp.compare(compare) == 0)) && std::getline(file, temp)) {
std::size_t pos = temp.find(' ');
std::string name = temp.substr(0,pos);
std::string number = temp.substr(pos);
....
So, there is a point when in the file there is a line called "data_END". it is read correctly as i can see in the debug options. However the .compare() method wont return 0. As i use visual Studio i can also see the details and everything between the strings seems the same exept the capacity. in temp it is 31 and in compare it is 15. Does this make the difference between them? I can't get any further by myself and i would appreciate any help,
thanks guys! :)
Upvotes: 2
Views: 1508
Reputation: 311088
Try to rewrite this statement:)
while ((!(temp.compare(compare) == 0)) && std::getline(file, temp)) {
like
while ( std::getline(file, temp) && !( temp.compare(compare) == 0) ) {
That is at first you have to read a line of the file in string temp
and only then compare it with string compare
.
The other problem can be with CR symbol in the text file. That is each line is ended with pair CR ('\r') + LF ('\n'). In this case you should remove it from string temp.
For example you can write
while ( std::getline(file, temp) && !( temp.compare(0, compare.size(), compare) == 0) ) {
Upvotes: 2