TransWikia.com

Is there any input that would cause getline to fail in C++?

Stack Overflow Asked on January 25, 2021

Except for inputs like ctrl c etc.

To my understanding getline can only fail when reaching EOF, but I’m not very sure.

One Answer

The short answer is : Anything that cases the input stream to be in an error state.

An "error state" of a standard stream (derived from std::istream) is expressed in terms of one or more of std::ios_base::failbit, std::ios_base::eofbit (which is associated with end of file), or std::ios_base::badbit being set. ios_base is a base class - via the templated std::basic_ios class - of all standard streams.

If none of those error states are set, std::getline() (or the member function of std::istream named getline()) can cause either eofbit or failbit to be set.

Conversely, std::getline() will respond to existing error states, and fail to read input if they are set.

For information on what the error states mean (i.e. what causes them for standard streams, in general) see cppreference material on iostate.

For information on when std::getline() sets the error states, see cppreference material on std::getline(). Similarly, for information on when the getline() member of standard input streams set error states, see cppreference material on std::basic_istream::getline().

Correct answer by Peter on January 25, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP