首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::basic_istream::getline

basic_istream& getline( char_type* s, std::streamsize count );

(1)

?

basic_istream& getline( char_type* s, std::streamsize count, char_type delim );

(2)

?

从流中提取字符,直到行尾或指定的分隔符。delim...

第一个版本相当于getline(s, count, widen('\n'))...

表现为UnformattedInputFunction.在构造和检查哨兵对象之后,从*this并将它们存储在数组的连续位置,该数组的第一个元素由s,直到按%29所示的顺序进行%28测试为止:

  • 文件结束条件出现在输入序列%28中,在这种情况下setstate(eofbit)执行%29
  • 下一个可用字符c所确定的分隔符。Traits::eq(c, delim).分隔符提取为%28不像basic_istream::get()%29gcount(),但没有储存。
  • count-1字符已被提取%28,在这种情况下setstate(failbit)执行%29。

如果函数没有提取字符%28例如。如果count < 1%29,setstate(failbit)被处决了。

无论如何,如果count>0,然后存储一个空字符。CharT()到数组的下一个连续位置并进行更新。gcount()...

注记

因为条件#2是在条件#3之前进行测试的,所以完全适合缓冲区的输入行不会触发故障位。

由于终止字符被计算为提取的字符,空输入行不会触发故障位。

参数

s

-

pointer to the character string to store the characters to

count

-

size of character string pointed to by s

delim

-

delimiting character to stop the extraction at. It is extracted but not stored.

返回值

*this...

例外

failure如果发生错误%28,则错误状态标志不是goodbit29%和exceptions()将被抛向那个州。

如果内部操作抛出异常,则会捕获该操作,并且badbit已经设定好了。如果exceptions()设置为badbit,异常将被重新抛出。

二次

代码语言:javascript
复制
#include <iostream>
#include <sstream>
#include <vector>
#include <array>
 
int main()
{
    std::istringstream input("abc|def|gh");
    std::vector<std::array<char, 4>> v;
 
    // note: the following loop terminates when std::ios_base::operator bool()
    // on the stream returned from getline() returns false
    for (std::array<char, 4> a; input.getline(&a[0], 4, '|'); ) {
        v.push_back(a);
    }
 
    for (auto& a : v) {
        std::cout << &a[0] << '\n';
    }
}

二次

产出:

二次

代码语言:javascript
复制
abc
def
gh

二次

另见

getline

read data from an I/O stream into a string (function template)

operator>>

extracts formatted data (public member function)

get

extracts characters (public member function)

read

extracts blocks of characters (public member function)

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com