std :: getline()将回车符\ r读入字符串,如何避免这种情况? [英] std::getline() reads carriage return \r into the string, how to avoid that?

查看:281
本文介绍了std :: getline()将回车符\ r读入字符串,如何避免这种情况?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从一个tcode文件中读取所有属性,该文件看起来像下面的一个 Stern (英语:Star)对象.我需要将字符串"leer" 替换为" ,但是也可以有一个有效的字符串,不应将其替换为"

I need to read all attributes from a tеxt file that looks like the following for one Stern (engl.: Star) object. I need to replace the string "leer" with "" but there can also be a valid string which shouldn't be replaced with "".

即,对于另一个 Stern 对象,也可能是"leer" ,而不是"Sol" .

I.e for another Stern object there could be "leer" instead of "Sol" as well.

问题:
问题在于它不会用" 替换"leer" .似乎将"leer \\ r" 保存在对象中,而不是仅将"leer \ 保存在对象中,但是我尝试替换" leer \\ r",但仍然无法正常工作.

Problem:
The problem is it doesn't replace the "leer" with the "". And it seems like it saves "leer\\r" in the object instead of only "leer" but I tried to replace "leer\\r" as well and it still doesn`t work.

这是应读取的文本文件中的一个 Stern :

This is one Stern in the text file that should be read:

0
Sol
0.000005
0.000000
0.000000
leer
1
0

这是我的 operator>> 来阅读:

istream& operator>>(istream& is, Stern& obj)
{
    string dummy;
    is >> obj.m_ID;
    getline(is, dummy);
    getline(is, obj.m_Bez);

    if (obj.m_Bez == "leer")
        obj.m_Bez = "";

    is >> obj.m_xKoord >> obj.m_yKoord >> obj.m_zKoord;
    getline(is,dummy);
    getline(is,obj.m_Sternbild);

    if (obj.m_Sternbild == "leer")
        obj.m_Sternbild = "";

    is >> obj.m_Index >> obj.m_PrimID;

    return is;
}

Stern.h:

#ifndef STERN_H
#define STERN_H
#include <string>
#include <iostream>

using namespace std;

class Stern
{
public:
    Stern();
    // 2.a)
    //Stern(int m_ID, string m_Bez, float m_xKoord, float m_yKoord, float m_zKoord, string m_Sternbild, int m_Index, int m_PrimID); 
    virtual ~Stern();

    void print() const; // 1.b)
    friend ostream& operator<<(ostream& os, const Stern& obj); // 1.b)i.
    friend istream& operator>>(istream& is, Stern& obj);


private:
    int m_ID;
    string m_Bez;
    float m_xKoord;
    float m_yKoord;
    float m_zKoord;
    string m_Sternbild;
    int m_Index;
    int m_PrimID;
};

#endif /* STERN_H */

推荐答案

问题是在Windows中,换行符表示为 CR + LF ,即:"\ r \ n" ,而在Unix中,它是 LF ,仅"\ n" .
您的 std :: getline(...)命令一直读到"leer \ r \ n" 中的"\ n" ,您生成的字符串将是:

The problem is that in Windows a newline is represented as CR + LF which is: "\r\n" and in Unix it is LF which is just "\n".
Your std::getline(...) command is reading till the "\n" in "leer\r\n", your resulting string will be:

"leer\r"

要解决此问题并在Unix/Windows之间转换文件,有2个工具 dos2unix unix2dos .Ubuntu等效项是 fromdos todos ,您将需要 fromdos 将Windows文本文件转换为Unix文本文件.

To solve this problem and convert files between Unix/Windows there are the 2 tools dos2unix and unix2dos. The Ubuntu equivalents are fromdos and todos, you will need fromdos to convert your Windows text file to a Unix text file.

要测试文件是否使用 CR + LF LF ,您可以执行以下操作:

To test wether a file uses CR + LF or LF you can do:

dos2unix < myfile.txt | cmp -s - myfile.txt

已在 Unix&Linux StackExchange网站.

似乎将"leer \\ r" 保存在对象中,而不是仅将"leer" 保存在对象中,但是我尝试替换"leer \\r" ,但仍然无法正常工作.我仍然不明白为什么我的 if(obj.m_Sternbild =="leer \\ r")不能正常工作,因为imo应该可以正常工作吗?

And it seems like it saves "leer\\r" in the object instead of only "leer" but I tried to replace "leer\\r" as well and it still doesn`t work. I still cant understand why my if (obj.m_Sternbild == "leer\\r") didn`t work because imo it should have worked?

应该是:

if (obj.m_Sternbild == "leer\r")

不转义反斜杠 \ ,因为将 \ r 读入字符串.

without escaping the backslash \, because \r is read into the string.

这篇关于std :: getline()将回车符\ r读入字符串,如何避免这种情况?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆