Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将get line()与多种类型的行尾字符一起使用_C++_Delimiter_Getline - Fatal编程技术网

C++ 将get line()与多种类型的行尾字符一起使用

C++ 将get line()与多种类型的行尾字符一起使用,c++,delimiter,getline,C++,Delimiter,Getline,我使用std::getline()的方式如下: 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; //

我使用std::getline()的方式如下:

 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参数的示例

如果有人能帮我举个例子或者用不同的方法,那就太好了。 谢谢

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


。。。然后使用中的读取删除了所有\r字符的文件。

\r\n
是您缺少的字符。可能有帮助:我隐约记得在处理混合平台时,在调用
std::getline
后,有另一个步骤删除了“\r”。这与问题无关,但是…:你到底为什么要使用
iss
?另外,我怀疑您对
clear()
的作用做出了错误的假设:虽然
clear()
从容器中删除元素,但它只清除
std::istream
的错误标志。在我进入循环(未显示)后,我标记了后续行。感谢您的帮助。我在这里找到了一篇很好的文章,它展示了如何创建我自己的定制getLine方法(类似于您在这里看到的),它解决了所有三种类型。
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;
};
std::ifstream fin("foo");
normalizebuf  sbuf(fin.rdbuf());
std::istream  in(&sbuf);