简体   繁体   English

MSVC 和 g++ 中的不同行尾

[英]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)赢得 10,视觉工作室 2019(MSVC)
  • under WSL, ubuntu 20.04, g++在 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.使用此代码 MSVC 不会打印空行,g++ 会。


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

With this code MSVC finds empty lines and g++ doesnt.使用此代码,MSVC 会找到空行,而 g++ 不会。


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

With this code g++ finds empty lines, MSVC doesnt使用此代码 g++ 找到空行,MSVC 没有

  1. Is it the Linux kernel that changes line endings or the compiler?是改变行尾还是编译器的 Linux kernel ?
  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.您的困难源于您在同一台机器上混合 Windows 和 Linux 行尾。 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. WSL 是一个类 Linux 环境,在 WSL 上处理 Windows 文件与在真正的 Linux 机器上处理它们没有什么不同,即有问题。

std::getline strips the \n (0x0A) line endings, and additionally in MSVC, reading a file in text mode automatically strips the \r (0x0D) characters. std::getline \n (0x0A) 行尾,另外在 MSVC 中,以文本模式读取文件会自动去除\r (0x0D) 字符。 The latter does not happen on Linux.后者不会发生在 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.因此,在非 Windows 平台上读取 Windows 文本文件(以\r\n行结尾)将删除\n但将\r留在行尾。

If you want to handle that situation, you can strip the trailing \r manually.如果你想处理这种情况,你可以手动去除尾随的\r 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.调试时以二进制模式打印出该line通常很有帮助,因为\r\n是不可见字符。

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

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