Namespaces
Variants
Views
Actions

std::basic_istream::getline

From cppreference.com
< cpp‎ | io‎ | basic istream
Revision as of 10:47, 21 April 2013 by SuperBoi45 (Talk | contribs)

Template:ddcl list begin <tr class="t-dcl ">

<td >
basic_istream& getline( char_type* s, std::streamsize count );
</td>

<td > (1) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl ">

<td >
basic_istream& getline( char_type* s, std::streamsize count, char_type delim );
</td>

<td > (2) </td> <td class="t-dcl-nopad"> </td> </tr> Template:ddcl list end

1) Extracts characters from stream until the end of line (equivalent to getline(s, count, widen(’\n’)))

2) Extracts characters from stream until the specified delimiter.

Behaves as UnformattedInputFunction. After constructing and checking the sentry object, extracts characters from *this and stored them in successive locations of the array whose first element is pointed to by s until any of the following occurs: (tested in the order shown)

  • end of file condition occurs in the input sequence (in which case setstate(eofbit) is executed)
  • the next available character c is the delimiter, as determined by Traits::eq(c, delim). The delimiter is extracted (unlike basic_istream::get()) and counted towards gcount(), but is not stored.
  • count-1 characters have been extracted (in which case setstate(failbit) is executed).

If the function extracts no characters (e.g. if count < 1), setstate(failbit) is executed.

In any case, if count>0, it then stores a null character CharT() into the next successive location of the array and updates gcount().

Contents

Notes

Because condition #2 is tested before condition #3, the input line that exactly fits the buffer, does not trigger failbit.

Because the terminating character is counted as extracted character, empty input line does not trigger failbit.

Parameters

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.

Return value

*this

Example

#include <iostream>
#include <sstream>
#include <vector>
#include <array>
 
int main()
{
    std::istringstream input("abc|def|gh");
    std::vector<std::array<char, 4>> v;
 
    for (std::array<char, 4> a; input.getline(&a[0], 4, '|'); ) {
        v.push_back(a);
    }
 
    for (auto& a : v) {
        std::cout << &a[0] << '\n';
    }
}

Output:

abc
def
gh

See also

Template:cpp/io/basic istream/dcl list operator gtgtTemplate:cpp/io/basic istream/dcl list getTemplate:cpp/io/basic istream/dcl list read
read data from an I/O stream into a string
(function)