Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/string/basic string/getline"

From cppreference.com
< cpp‎ | string‎ | basic string
(Notes: Made the presentation a bit more friendly)
m (langlinks, cosmetic)
Line 30: Line 30:
  
 
@1@ Behaves as {{named req|UnformattedInputFunction}}, except that {{tt|input.gcount()}} is not affected. After constructing and checking the sentry object, performs the following:
 
@1@ Behaves as {{named req|UnformattedInputFunction}}, except that {{tt|input.gcount()}} is not affected. After constructing and checking the sentry object, performs the following:
:@1@ Calls {{c|str.erase()}}
+
:* Calls {{c|str.erase()}}
:@2@ Extracts characters from {{tt|input}} and appends them to {{tt|str}} until one of the following occurs (checked in the order listed)
+
:* Extracts characters from {{tt|input}} and appends them to {{tt|str}} until one of the following occurs (checked in the order listed)
 
::@a@ end-of-file condition on {{tt|input}}, in which case, {{tt|getline}} sets {{ltt|cpp/io/ios_base/iostate|eofbit}}.
 
::@a@ end-of-file condition on {{tt|input}}, in which case, {{tt|getline}} sets {{ltt|cpp/io/ios_base/iostate|eofbit}}.
 
::@b@ the next available input character is {{tt|delim}}, as tested by {{c|Traits::eq(c, delim)}}, in which case the delimiter character is extracted from {{tt|input}}, but is not appended to {{tt|str}}.
 
::@b@ the next available input character is {{tt|delim}}, as tested by {{c|Traits::eq(c, delim)}}, in which case the delimiter character is extracted from {{tt|input}}, but is not appended to {{tt|str}}.
 
::@c@ {{c|str.max_size()}} characters have been stored, in which case {{tt|getline}} sets {{ltt|cpp/io/ios_base/iostate|failbit}} and returns.
 
::@c@ {{c|str.max_size()}} characters have been stored, in which case {{tt|getline}} sets {{ltt|cpp/io/ios_base/iostate|failbit}} and returns.
:@3@ If no characters were extracted for whatever reason (not even the discarded delimiter), {{tt|getline}} sets {{ltt|cpp/io/ios_base/iostate|failbit}} and returns.
+
:* If no characters were extracted for whatever reason (not even the discarded delimiter), {{tt|getline}} sets {{ltt|cpp/io/ios_base/iostate|failbit}} and returns.
  
 
@2@ Same as {{c|getline(input, str, input.widen('\n'))}}, that is, the default delimiter is the endline character.
 
@2@ Same as {{c|getline(input, str, input.widen('\n'))}}, that is, the default delimiter is the endline character.
Line 92: Line 92:
 
{{dsc end}}
 
{{dsc end}}
  
[[de:cpp/string/basic string/getline]]
+
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
[[es:cpp/string/basic string/getline]]
+
[[fr:cpp/string/basic string/getline]]
+
[[it:cpp/string/basic string/getline]]
+
[[ja:cpp/string/basic string/getline]]
+
[[pt:cpp/string/basic string/getline]]
+
[[ru:cpp/string/basic string/getline]]
+
[[zh:cpp/string/basic string/getline]]
+

Revision as of 07:29, 7 August 2020

 
 
 
std::basic_string
Member functions
Element access
Iterators
Capacity
Modifiers
Search
Operations
Constants
Non-member functions
I/O
getline
Comparison
(until C++20)(until C++20)(until C++20)(until C++20)(until C++20)(C++20)
Numeric conversions
(C++11)(C++11)(C++11)
(C++11)(C++11)
(C++11)(C++11)(C++11)
(C++11)
(C++11)
Literals
Helper classes
Deduction guides (C++17)

 
Defined in header <string>
template< class CharT, class Traits, class Allocator >

std::basic_istream<CharT,Traits>& getline( std::basic_istream<CharT,Traits>& input,
                                           std::basic_string<CharT,Traits,Allocator>& str,

                                           CharT delim );
(1)
template< class CharT, class Traits, class Allocator >

std::basic_istream<CharT,Traits>& getline( std::basic_istream<CharT,Traits>&& input,
                                           std::basic_string<CharT,Traits,Allocator>& str,

                                           CharT delim );
(1) (since C++11)
template< class CharT, class Traits, class Allocator >

std::basic_istream<CharT,Traits>& getline( std::basic_istream<CharT,Traits>& input,

                                           std::basic_string<CharT,Traits,Allocator>& str );
(2)
template< class CharT, class Traits, class Allocator >

std::basic_istream<CharT,Traits>& getline( std::basic_istream<CharT,Traits>&& input,

                                           std::basic_string<CharT,Traits,Allocator>& str );
(2) (since C++11)

getline reads characters from an input stream and places them into a string:

1) Behaves as UnformattedInputFunction, except that input.gcount() is not affected. After constructing and checking the sentry object, performs the following:
  • Calls str.erase()
  • Extracts characters from input and appends them to str until one of the following occurs (checked in the order listed)
a) end-of-file condition on input, in which case, getline sets eofbit.
b) the next available input character is delim, as tested by Traits::eq(c, delim), in which case the delimiter character is extracted from input, but is not appended to str.
c) str.max_size() characters have been stored, in which case getline sets failbit and returns.
  • If no characters were extracted for whatever reason (not even the discarded delimiter), getline sets failbit and returns.
2) Same as getline(input, str, input.widen('\n')), that is, the default delimiter is the endline character.

Contents

Parameters

input - the stream to get data from
str - the string to put the data into
delim - the delimiter character

Return value

input

Notes

When consuming whitespace-delimited input (e.g. int n; std::cin >> n;) any whitespace that follows, including a newline character, will be left on the input stream. Then when switching to line-oriented input, the first line retrieved with getline will be just that whitespace. In the likely case that this is unwanted behaviour, possible solutions include:

Example

The following example demonstrates how to use getline function to read user's input and how to process file line by line.

#include <string>
#include <iostream>
#include <sstream>
 
int main()
{
    // greet the user
    std::string name;
    std::cout << "What is your name? ";
    std::getline(std::cin, name);
    std::cout << "Hello " << name << ", nice to meet you.\n";
 
    // read file line by line
    std::istringstream input;
    input.str("1\n2\n3\n4\n5\n6\n7\n");
    int sum = 0;
    for (std::string line; std::getline(input, line); ) {
        sum += std::stoi(line);
    }
    std::cout << "\nThe sum is: " << sum << "\n";
}

Possible output:

What is your name? John Q. Public
Hello John Q. Public, nice to meet you.
 
The sum is 28

See also

extracts characters until the given character is found
(public member function of std::basic_istream<CharT,Traits>) [edit]