c++ - 两个连续的换行符作为分隔符 - C/C++

标签 c++ getline

我正在下面编写一个 getline 函数,但我希望分隔字符是两个连续的换行符,这样函数一旦到达空白行就会停止读取。

in.getline(temp, 127, delimiter);

可以用 getline 来做到这一点吗?

最佳答案

不,但是一个简单(不像我一开始想象的那么简单)循环就可以做到。

std::string s, temp;
while (std::getline(in, temp, delimiter) && !temp.empty())
    s += temp, s += delimiter;
if (!s.empty())
    s.resize(s.size() - 1) // or s.pop_back() if C++11

关于c++ - 两个连续的换行符作为分隔符 - C/C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7896265/

相关文章:

c++ - 使用 getline 给出错误 : no matching function for call to ‘getline(std::istream&, const string&)’

c++ - 错误 : expected primary-expression before ‘=’ token when using for loops

c++ - 结构体非类型模板参数+聚合初始化: standard in c++20 or not

c++ - 处理 std::wstring 和 std::string 之间的 UTF-8 编码字符串

C++ sscanf 在编译时抛出不相关类型的 "no suitable conversion"错误

c - 从不兼容的指针类型传递 getline() 的参数

c++ - 为长值数组重载 operator<

具有任意索引的数组的 C++ 类

c++ - 如果在某些输入之后使用 getline() 将不起作用

C++ getline inside while loop 缺少第一个字母。没有 cin.ignore 不工作?