简体   繁体   English

比较文本文件中的字符串

[英]Comparing strings from a text file

I am parsing a text file, and for some reason string::compare() isn't working as intended. 我正在解析一个文本文件,由于某种原因, string::compare()无法按预期工作。

The text file: http://pastebin.com/raw.php?i=WZDWmb56 文本文件: http : //pastebin.com/raw.php?i=WZDWmb56

The read function (called from inside while loop): 读取功能(从while循环内部调用):

string StopName = "***";
    bool Person::ReadOnePersonFromFile(ifstream& fin)
    {
        getline(fin,m_name);
        cout << m_name << endl;
        if( m_name == StopName )
            return false;
        fin >> m_id;
        fin.ignore(50,'\n');
        return true;
    }

Whenever "***" is reached, if( m_name == StopName ) doesn't return true . 每当到达"***"时, if( m_name == StopName )都不会返回true What is going on? 到底是怎么回事?

This function works on Windows (Visual Studio). 此功能在Windows(Visual Studio)上有效。 I am currently compiling this on Linux. 我目前正在Linux上对此进行编译。 Does this have anything to do with how the text is stored? 这与文本的存储方式有关吗?

看起来您在代码中比较的是StopName而不是Stop ,因此从未检查过“ ***”(假设StopStopName不是同一件事)。

Found my answer. 找到了我的答案。 I believe this is because of the way newline characters are encoded on Windows. 我相信这是由于Windows上的换行字符编码方式所致。

 Windows: \r\n  (CR + LF)
 Linux: \n 
 Mac: \r

I had to convert my Windows text file to a Unix text file with dos2unix . 我必须使用dos2unix将Windows文本文件转换为Unix文本文件。 The program works fine. 该程序工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM