0

I am trying to read a file line by line and compare it to string in the code. But somehow the following code is not giving expected result. I do not follow what am I missing during the comparison:

CODE

int main(int argc, char** argv)
{
    std::string filePath="E:\\data\\stopfile.txt";
    std::string line;
    std::ifstream myfile;
    std::string test="ball";
    myfile.open(filePath.c_str());
    if(myfile.is_open()){
        while(getline(myfile,line)){
            std::cout<<line<<std::endl;
            if(!line.compare(test)){
                std::cout<<"SUCCESS"<<std::endl;
            }
            else{
                std::cout<<"FAIL"<<std::endl;
            }
        }
    }
    myfile.close();

    if(!test.compare("ball")){
        std::cout<<"SUCCESS"<<std::endl;
    }
}

OUTPUT

apple
FAIL
ball
FAIL
cat
FAIL
SUCCESS

I expect the program to print SUCCESS after the line of "ball". But the comparison does not seem to be success.

I have also tried the comparison condition to

if(!line.compare(test.c_str())){

Still the result is the same.

lonstud
  • 525
  • 2
  • 19
  • Do the strings in your file contain only the words without spaces (one word per line)? – Galik Mar 21 '15 at 05:45
  • Clean Project and rebuilt it. Still doesnt work for me. Any ideas how to proceed further to debug this. I am using eclipse workspace on windows 7 machine with cygwin gcc toolchain – lonstud Mar 21 '15 at 05:47
  • yes. One word per line, no spaces. I recreated the file as well just now. – lonstud Mar 21 '15 at 05:48
  • 1
    I would be tempted to print out the lengths of the strings you read in from the file. Is your file editor adding Unix or Mac line endings maybe? – Galik Mar 21 '15 at 05:49
  • thank you Galik. File editor was causing the problem. I am using Notepad++ and string length was 1 character more than it should be. Created the file again through command prompt and it works fine now. – lonstud Mar 21 '15 at 05:54
  • @galik can you suggest how to make sure my program works correctly by checking if there is extra spacing at the end of line when file is created through notepad++ or some other editor in future. – lonstud Mar 21 '15 at 05:55
  • 1
    If I don't control the source of the file I will often trim() the data as I read it in using functions like this: http://stackoverflow.com/a/25385766/3807729 – Galik Mar 21 '15 at 06:06
  • Works like a charm. Thank you very much for your help. Please put your solution as answer so that I may close this question. – lonstud Mar 21 '15 at 06:09
  • I assume you are using cygwin. Notepad++ has the possibility to choose line endings of your choice. cygwin has a page dedicated to the problem, too: https://cygwin.com/cygwin-ug-net/using-textbinary.html – Peter - Reinstate Monica Mar 21 '15 at 07:34
  • Yes, Peter. I am using cygwin. Thank you for the link provided. – lonstud Mar 21 '15 at 09:45

1 Answers1

2

It seems you are picking up line endings not appropriate for your platform. If I don't control the source of the file I will often trim() the data as I read it in using functions like this:

https://stackoverflow.com/a/25385766/3807729

const char* ws = " \t\n\r\f\v";

// trim from end (right)
inline std::string& rtrim(std::string& s, const char* t = ws)
{
    s.erase(s.find_last_not_of(t) + 1);
    return s;
}

// trim from beginning (left)
inline std::string& ltrim(std::string& s, const char* t = ws)
{
    s.erase(0, s.find_first_not_of(t));
    return s;
}

// trim from both ends (left & right)
inline std::string& trim(std::string& s, const char* t = ws)
{
    return ltrim(rtrim(s, t), t);
}

// ...

while(getline(myfile,line))
{
    trim(line);
    // ...
}
Community
  • 1
  • 1
Galik
  • 47,303
  • 4
  • 80
  • 117