简体   繁体   中英

Different line endings in MSVC and g++

I'm trying to parse a text from file, in which I have to detect empty lines. I'm running the code in 2 places:

  • win 10, visual studio 2019(MSVC)
  • under WSL, ubuntu 20.04, g++

Same computer, same file, same code.

while (getline(inputFile, line))
{
    if (line.length() == 1)
    {
        std::cout << "Empty line" << std::endl;
    }
/*blabla*/

With this code MSVC doesnt print empty lines, g++ does.


if (line.empty())
{
    std::cout << "Empty line" << std::endl;
}

With this code MSVC finds empty lines and g++ doesnt.


if (int(line[0]) == 10 || int(line[0]) == 13)
{
    std::cout << "Empty line" << std::endl;
}

With this code g++ finds empty lines, MSVC doesnt

  1. Is it the Linux kernel that changes line endings or the compiler?
  2. What is the proper way to always detect line endings and empty line on every system?

Your difficulties stem from the fact that you're mixing Windows and Linux line endings on the same machine. WSL is a Linux-like environment, and processing Windows files on WSL is no different than processing them on a real Linux machine, ie, problematic.

std::getline strips the \n (0x0A) line endings, and additionally in MSVC, reading a file in text mode automatically strips the \r (0x0D) characters. The latter does not happen on Linux.

So reading a Windows text file (with \r\n line endings) on a non-Windows platform will strip \n but leave \r at the end of the line.

If you want to handle that situation, you can strip the trailing \r manually. For example

while (std::getline(inputFile, line))
{
    if (!line.empty() && line.back() == '\r')
    {
        line.pop_back();
    }
    if (line.empty())
    {
        std::cout << "Empty line" << std::endl;
    }

It is usually helpful to print out the line in binary mode when debugging, because \r and \n are invisible characters.

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.

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