Skip to content

Instantly share code, notes, and snippets.

@esnosy
Created September 28, 2023 01:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save esnosy/f95d99443b3749adfd6aafe39f132dbc to your computer and use it in GitHub Desktop.
Save esnosy/f95d99443b3749adfd6aafe39f132dbc to your computer and use it in GitHub Desktop.
A skip_line function for std::ifstream
static void skip_line(std::ifstream &ifs)
{
// Thanks to Eljay and CPPReference
// https://stackoverflow.com/questions/53693218/how-to-skip-a-line-of-standard-input#comment94242047_53693218
// https://en.cppreference.com/w/cpp/string/basic_string/getline#Notes
// ifs.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
// also thanks to https://stackoverflow.com/a/72365435/8094047
// and https://jdhao.github.io/2018/12/08/newline_vim_python_sublime_notepad/
// Skip all characters until newline or carriage return
char c = '\0';
while (c != '\n' && c != '\r') {
ifs.get(c);
}
// Skip all whitespace
ifs >> std::ws;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment