Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ifstream stream;
- stream.exceptions(ifstream::failbit | ifstream::badbit);
- stream.open(filename.c_str(), ios::binary);
- char buffer[10];
- stream.read(buffer, sizeof(buffer));
- char buffer[10];
- stream.read(buffer, sizeof(buffer));
- if (stream.eof()) // or stream.gcount() != sizeof(buffer)
- // handle eof myself
- char buffer[10];
- stream.read(buffer, sizeof(buffer));
- char buffer[10];
- streamsize numRead = stream.readsome(buffer, sizeof(buffer));
- 137
- ifstream input("myfile.txt");
- int value;
- input >> value;
- char buffer[10];
- streamsize num_read = stream.rdbuf()->sgetn(buffer, sizeof(buffer));
- #include <iostream>
- #include <fstream>
- using namespace std;
- streamsize readeof(ifstream &stream, char* buffer, streamsize count)
- {
- // This consistently fails on gcc (linux) 4.8.1 with failbit set on read
- // failure. This apparently never fails on VS2010 and VS2013 (Windows 7)
- streamsize reads = stream.rdbuf()->sgetn(buffer, count);
- // This rarely sets failbit on VS2010 and VS2013 (Windows 7) on read
- // failure of the previous sgetn()
- stream.rdstate();
- // On gcc (linux) 4.8.1 and VS2010/VS2013 (Windows 7) this consistently
- // sets eofbit when stream is EOF for the conseguences of sgetn(). It
- // should also throw if exceptions are set, or return on the contrary,
- // and previous rdstate() restored a failbit on Windows. On Windows most
- // of the times it sets eofbit even on real read failure
- stream.peek();
- return reads;
- }
- #define BIGGER_BUFFER_SIZE 200000000
- int main(int argc, char* argv[])
- {
- ifstream stream;
- stream.exceptions(ifstream::badbit | ifstream::failbit);
- stream.open("<big file on usb stick>", ios::binary);
- char *buffer = new char[BIGGER_BUFFER_SIZE];
- streamsize reads = readeof(stream, buffer, BIGGER_BUFFER_SIZE);
- if (stream.eof())
- cout << "eof" << endl << flush;
- delete buffer;
- return 0;
- }
Add Comment
Please, Sign In to add comment