Guest User

Untitled

a guest
Feb 25th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. ifstream stream;
  2. stream.exceptions(ifstream::failbit | ifstream::badbit);
  3. stream.open(filename.c_str(), ios::binary);
  4.  
  5. char buffer[10];
  6. stream.read(buffer, sizeof(buffer));
  7.  
  8. char buffer[10];
  9. stream.read(buffer, sizeof(buffer));
  10. if (stream.eof()) // or stream.gcount() != sizeof(buffer)
  11. // handle eof myself
  12.  
  13. char buffer[10];
  14. stream.read(buffer, sizeof(buffer));
  15.  
  16. char buffer[10];
  17. streamsize numRead = stream.readsome(buffer, sizeof(buffer));
  18.  
  19. 137
  20.  
  21. ifstream input("myfile.txt");
  22.  
  23. int value;
  24. input >> value;
  25.  
  26. char buffer[10];
  27. streamsize num_read = stream.rdbuf()->sgetn(buffer, sizeof(buffer));
  28.  
  29. #include <iostream>
  30. #include <fstream>
  31.  
  32. using namespace std;
  33.  
  34. streamsize readeof(ifstream &stream, char* buffer, streamsize count)
  35. {
  36. // This consistently fails on gcc (linux) 4.8.1 with failbit set on read
  37. // failure. This apparently never fails on VS2010 and VS2013 (Windows 7)
  38. streamsize reads = stream.rdbuf()->sgetn(buffer, count);
  39.  
  40. // This rarely sets failbit on VS2010 and VS2013 (Windows 7) on read
  41. // failure of the previous sgetn()
  42. stream.rdstate();
  43.  
  44. // On gcc (linux) 4.8.1 and VS2010/VS2013 (Windows 7) this consistently
  45. // sets eofbit when stream is EOF for the conseguences of sgetn(). It
  46. // should also throw if exceptions are set, or return on the contrary,
  47. // and previous rdstate() restored a failbit on Windows. On Windows most
  48. // of the times it sets eofbit even on real read failure
  49. stream.peek();
  50.  
  51. return reads;
  52. }
  53.  
  54. #define BIGGER_BUFFER_SIZE 200000000
  55.  
  56. int main(int argc, char* argv[])
  57. {
  58. ifstream stream;
  59. stream.exceptions(ifstream::badbit | ifstream::failbit);
  60. stream.open("<big file on usb stick>", ios::binary);
  61.  
  62. char *buffer = new char[BIGGER_BUFFER_SIZE];
  63.  
  64. streamsize reads = readeof(stream, buffer, BIGGER_BUFFER_SIZE);
  65.  
  66. if (stream.eof())
  67. cout << "eof" << endl << flush;
  68.  
  69. delete buffer;
  70.  
  71. return 0;
  72. }
Add Comment
Please, Sign In to add comment