Consider the following code example:
#include <iostream>
using namespace std;
int main()
{
istreambuf_iterator<char> eos;
istreambuf_iterator<char> iit(cin.rdbuf());
int i;
for (i = 0; iit != eos; ++i, ++iit) {
cout << *iit;
}
cout << endl << i << endl;
}
And an input file containing the following: "foo \ xffbar":
$ hexdump testin
0000000 66 6f 6f ff 62 61 72
0000007
Now for the test using clang lib ++ vs gnu libstd ++:
$ make test
clang++ -std=c++11 -stdlib=libc++ -Wall -stdlib=libc++ -o bug-libcc bug.cpp
clang++ -std=c++11 -stdlib=libc++ -Wall -stdlib=libstdc++ -o bug-libstd bug.cpp
./bug-libcc < testin
foo
3
./bug-libstd < testin
foo bar
7
As you can see, the libC ++ version considers 0xff to be the end of the stream and it stops reading. Thus, this leads to two questions.
1) Is this a bug in libC ++ that I should report? My Google searches were not found.
2) Is there a good way around this problem?
EDIT
The following code works:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream ifs ("testin", ios::binary);
istreambuf_iterator<char> eos;
istreambuf_iterator<char> iit(ifs.rdbuf());
int i;
for (i = 0; iit != eos; ++i, ++iit) {
cout << *iit;
}
cout << endl << i << endl;
}
I suppose this is a binary conversion problem, but that does not explain why libstdc ++ is working correctly.
EDIT2
Using a file without binaries works fine:
ifstream ifs ("testin");
, - . , cin, .