Difference between revisions of "cpp/string/basic string/getline"
m (→Synopsis: Apply {dcl rev multi}.) |
|||
(3 intermediate revisions by 2 users not shown) | |||
Line 3: | Line 3: | ||
{{dcl begin}} | {{dcl begin}} | ||
{{dcl header|string}} | {{dcl header|string}} | ||
− | {{dcl | + | {{dcl|num=1|1= |
− | + | ||
template< class CharT, class Traits, class Allocator > | template< class CharT, class Traits, class Allocator > | ||
std::basic_istream<CharT, Traits>& | std::basic_istream<CharT, Traits>& | ||
− | getline( std::basic_istream<CharT, Traits>& input, | + | getline( std::basic_istream<CharT, Traits>& input, |
− | + | std::basic_string<CharT, Traits, Allocator>& str, CharT delim ); | |
− | | | + | }} |
+ | {{dcl|since=c++11|num=2|1= | ||
template< class CharT, class Traits, class Allocator > | template< class CharT, class Traits, class Allocator > | ||
std::basic_istream<CharT, Traits>& | std::basic_istream<CharT, Traits>& | ||
− | getline( std::basic_istream<CharT, Traits>&& input, | + | getline( std::basic_istream<CharT, Traits>&& input, |
− | + | std::basic_string<CharT, Traits, Allocator>& str, CharT delim ); | |
}} | }} | ||
− | {{dcl | + | {{dcl|num=3|1= |
− | + | ||
template< class CharT, class Traits, class Allocator > | template< class CharT, class Traits, class Allocator > | ||
std::basic_istream<CharT, Traits>& | std::basic_istream<CharT, Traits>& | ||
− | getline( std::basic_istream<CharT, Traits>& input, | + | getline( std::basic_istream<CharT, Traits>& input, |
− | + | std::basic_string<CharT, Traits, Allocator>& str ); | |
− | | | + | }} |
+ | {{dcl|since=c++11|num=4|1= | ||
template< class CharT, class Traits, class Allocator > | template< class CharT, class Traits, class Allocator > | ||
std::basic_istream<CharT, Traits>& | std::basic_istream<CharT, Traits>& | ||
− | getline( std::basic_istream<CharT, Traits>&& input, | + | getline( std::basic_istream<CharT, Traits>&& input, |
− | + | std::basic_string<CharT, Traits, Allocator>& str ); | |
}} | }} | ||
{{dcl end}} | {{dcl end}} | ||
Line 31: | Line 31: | ||
{{tt|getline}} reads characters from an input stream and places them into a string: | {{tt|getline}} reads characters from an input stream and places them into a string: | ||
− | @1@ Behaves as {{named req|UnformattedInputFunction}}, except that {{c|input.gcount()}} is not affected. After constructing and checking the sentry object, performs the following: | + | @1,2@ Behaves as {{named req|UnformattedInputFunction}}, except that {{c|input.gcount()}} is not affected. After constructing and checking the sentry object, performs the following: |
− | :@1@ Calls {{c|str.erase()}} | + | :@1@ Calls {{c|str.erase()}}. |
:@2@ Extracts characters from {{c|input}} and appends them to {{c|str}} until one of the following occurs (checked in the order listed) | :@2@ Extracts characters from {{c|input}} and appends them to {{c|str}} until one of the following occurs (checked in the order listed) | ||
::@a@ end-of-file condition on {{c|input}}, in which case, {{tt|getline}} sets {{ltt|cpp/io/ios_base/iostate|eofbit}}. | ::@a@ end-of-file condition on {{c|input}}, in which case, {{tt|getline}} sets {{ltt|cpp/io/ios_base/iostate|eofbit}}. | ||
Line 39: | Line 39: | ||
:@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. | :@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. | ||
− | @ | + | @3,4@ Same as {{c|getline(input, str, input.widen('\n'))}}, that is, the default delimiter is the endline character. |
===Parameters=== | ===Parameters=== | ||
Line 53: | Line 53: | ||
===Notes=== | ===Notes=== | ||
When consuming whitespace-delimited input (e.g. {{c|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 {{tt|getline}} will be just that whitespace. In the likely case that this is unwanted behaviour, possible solutions include: | When consuming whitespace-delimited input (e.g. {{c|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 {{tt|getline}} will be just that whitespace. In the likely case that this is unwanted behaviour, possible solutions include: | ||
− | * An explicit extraneous initial call to {{tt|getline}} | + | * An explicit extraneous initial call to {{tt|getline}}. |
− | * Removing consecutive whitespace with {{c|std::cin >> std::ws}} | + | * Removing consecutive whitespace with {{c|std::cin >> std::ws}}. |
− | * Ignoring all leftover characters on the line of input with {{c|cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');}} | + | * Ignoring all leftover characters on the line of input with {{c|cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');}}. |
===Example=== | ===Example=== | ||
{{example | {{example | ||
|p=true | |p=true | ||
− | |The following example demonstrates how to use the {{tt|getline}} function to read user input, and to process a stream line by line, or by parts of a line using the {{ | + | |The following example demonstrates how to use the {{tt|getline}} function to read user input, and to process a stream line by line, or by parts of a line using the {{c|delim}} parameter. |
|code= | |code= | ||
− | |||
#include <iostream> | #include <iostream> | ||
#include <sstream> | #include <sstream> | ||
+ | #include <string> | ||
int main() | int main() | ||
Line 78: | Line 78: | ||
input.str("1\n2\n3\n4\n5\n6\n7\n"); | input.str("1\n2\n3\n4\n5\n6\n7\n"); | ||
int sum = 0; | int sum = 0; | ||
− | for (std::string line; std::getline(input, line); ) | + | for (std::string line; std::getline(input, line);) |
sum += std::stoi(line); | sum += std::stoi(line); | ||
std::cout << "\nThe sum is " << sum << ".\n\n"; | std::cout << "\nThe sum is " << sum << ".\n\n"; | ||
Line 85: | Line 85: | ||
std::istringstream input2; | std::istringstream input2; | ||
input2.str("a;b;c;d"); | input2.str("a;b;c;d"); | ||
− | for (std::string line; std::getline(input2, line, ';'); ) | + | for (std::string line; std::getline(input2, line, ';');) |
std::cout << line << '\n'; | std::cout << line << '\n'; | ||
} | } | ||
− | |p=true|output= | + | |p=true |
+ | |output= | ||
What is your name? John Q. Public | What is your name? John Q. Public | ||
Hello John Q. Public, nice to meet you. | Hello John Q. Public, nice to meet you. |
Latest revision as of 16:35, 12 May 2024
Defined in header <string>
|
||
template< class CharT, class Traits, class Allocator > std::basic_istream<CharT, Traits>& |
(1) | |
template< class CharT, class Traits, class Allocator > std::basic_istream<CharT, Traits>& |
(2) | (since C++11) |
template< class CharT, class Traits, class Allocator > std::basic_istream<CharT, Traits>& |
(3) | |
template< class CharT, class Traits, class Allocator > std::basic_istream<CharT, Traits>& |
(4) | (since C++11) |
getline
reads characters from an input stream and places them into a string:
getline
sets failbit and returns.Contents |
[edit] Parameters
input | - | the stream to get data from |
str | - | the string to put the data into |
delim | - | the delimiter character |
[edit] Return value
input
[edit] 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:
- An explicit extraneous initial call to
getline
. - Removing consecutive whitespace with std::cin >> std::ws.
- Ignoring all leftover characters on the line of input with cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');.
[edit] Example
The following example demonstrates how to use the getline
function to read user input, and to process a stream line by line, or by parts of a line using the delim parameter.
#include <iostream> #include <sstream> #include <string> 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\n"; // use separator to read parts of the line std::istringstream input2; input2.str("a;b;c;d"); for (std::string line; std::getline(input2, line, ';');) std::cout << line << '\n'; }
Possible output:
What is your name? John Q. Public Hello John Q. Public, nice to meet you. The sum is 28. a b c d
[edit] Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 91 | C++98 | getline did not behave as an unformatted input function
|
behaves as an unformatted input function |
[edit] See also
extracts characters until the given character is found (public member function of std::basic_istream<CharT,Traits> )
|