将get line()与多种类型的行尾字符一起使用 [英] Using get line() with multiple types of end of line characters

查看:74
本文介绍了将get line()与多种类型的行尾字符一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以以下方式使用std :: getline():

I am using std::getline() in the following manner:

 std::fstream verify;
 verify.open(myURI.c_str());
 std::string countingLine;

  if(verify.is_open()){

        std::getline(verify, countingLine);

        std::istringstream iss(countingLine);
        size_t pos;

        // Check for the conventional myFile header.
        pos = iss.str().find("Time,Group,Percent,Sign,Focus");

        if(pos == std::string::npos){//its not there
            headerChk = false;
            this->setStatusMessage("Invalid header for myFile file");
            return 0;
        }

         // loop that does more validation

        iss.clear();

    }

问题是我在Mac上编码(并且Windows工具和Apple工具都会修改某些文件)。行尾的某些字符是\r而不是\n,因此我的文件字符串从不分成几行。我相信我还要检查第三个。我很难找到为多个endOfLine字符设置delim参数的示例。

The problem is I'm coding on a mac (and some files get modified with both windows tools and apple tools). Some end of line characters are \r instead of \n, so my file string is never broken into lines. I believe there is also a third one I should be checking for. I'm having trouble finding an example of setting up the delim parameter for multiple endOfLine characters.

如果有人可以帮助您解决该示例或其他方法,那将是很好的选择。
谢谢

If someone could help with that example or a different approach that would be great. Thanks

推荐答案

std :: getline()支持行尾字符。以文本模式打开文件时,系统的行尾序列将转换为一个行尾字符( \n )。但是,这不涉及其他系统的行尾字符序列。实际上,真正需要做的就是从剩下的输入中删除 \r 字符。删除字符的最佳方法可能是创建过滤流缓冲区。这是一个琐碎的,未经测试的并且可能很慢的示例(它没有缓冲,这意味着每个字符都有虚函数调用;这很可怕;创建缓冲版本并不难):

std::getline() only supports one end of line character. When opening a file in text mode, the system's end of line sequences are converted into one single end of line character (\n). However, this doesn't deal with end of line character sequences from other systems. Practically, all what really needs to be done is to remove the \r character from the input which remains. The best way to remove characters is probably to create a filtering stream buffer. Here is a trivial, untested, and probably slow one (it isn't buffering which means there is virtual function call for each individual character; this is horrific; creating a buffered version isn't much harder, though):

class normalizebuf
    : std::streambuf {
    std::streambuf* sbuf_;
    char            buffer_[1];
public:
    normalizebuf(std::streambuf* sbuf): sbuf_(sbuf) {}
    int underflow() {
        int c = this->sbuf_->sbumpc();
        while (c == std::char_traits<char>::to_int_type('\r')) {
            c = this->sbuf->sbumpc();
        }
        if (c != std::char_traits<char>::eof()) {
            this->buffer_[0] = std::char_traits<char>::to_char_type(c);
            this->setg(this->buffer_, this->buffer_, this->buffer_ + 1);
        }
        return c;
};

您可以将此过滤器与现有的流缓冲区配合使用,如下所示:

You'd use this filter with an existing stream buffer, something like this:

std::ifstream fin("foo");
normalizebuf  sbuf(fin.rdbuf());
std::istream  in(&sbuf);

...,然后在<$ c $中使用 c>读取已删除所有 \r 个字符的文件。

... and then you'd use in to read the file with all \r characters removed.

这篇关于将get line()与多种类型的行尾字符一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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