I am parsing a text file, and for some reason string::compare()
isn't working as intended.
The text file: http://pastebin.com/raw.php?i=WZDWmb56
The read function (called from inside while loop):
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
. What is going on?
This function works on Windows (Visual Studio). I am currently compiling this on Linux. Does this have anything to do with how the text is stored?
看起来您在代码中比较的是StopName
而不是Stop
,因此从未检查过“ ***”(假设Stop
和StopName
不是同一件事)。
Found my answer. I believe this is because of the way newline characters are encoded on 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
. The program works fine.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.