Skip to content

Instantly share code, notes, and snippets.

@AbigailBuccaneer
Last active November 18, 2015 17:02
Show Gist options
  • Save AbigailBuccaneer/86db884555cb821d60ad to your computer and use it in GitHub Desktop.
Save AbigailBuccaneer/86db884555cb821d60ad to your computer and use it in GitHub Desktop.
std::istream::ignore is inconsistent with regards to eof()
#include <iostream>
#include <sstream>
#include <fstream>
using namespace std;
int main(int argc, char** argv) {
string s("ab");
{
istringstream stream(s);
stream.ignore(1); stream.ignore(1);
cout << stream.good() << endl; // 1
}
{
istringstream stream(s);
stream.ignore(2);
cout << stream.good() << endl; // 0
}
{
ifstream stream("test.cc");
stream.seekg(0, ios_base::end);
size_t size = stream.tellg();
stream.seekg(0, ios_base::beg);
for (size_t i = 0; i < size; ++i) {
stream.ignore();
}
cout << stream.good() << endl; // 1
}
{
ifstream stream("test.cc");
stream.seekg(0, ios_base::end);
size_t size = stream.tellg();
stream.seekg(0, ios_base::beg);
stream.ignore(size);
cout << stream.good() << endl; // 0
}
}
@AbigailBuccaneer
Copy link
Author

$ g++ --version && g++ test.cc && ./a.out 
g++ (Ubuntu 5.2.1-22ubuntu2) 5.2.1 20151010
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

1
0
1
0

@AbigailBuccaneer
Copy link
Author

$ clang++ --version && clang++ test.cc && ./a.out 
Ubuntu clang version 3.6.2-1 (tags/RELEASE_362/final) (based on LLVM 3.6.2)
Target: x86_64-pc-linux-gnu
Thread model: posix
1
0
1
0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment