Why is C ++ ifstreams not readable from devices?

I knew that I should never have used C ++ io, the whole argument “security type” is a red herring (does anyone really think that this is one of the most pressing problems?). In any case, I did this, and discovered a strange difference between ifstream and FILE * s and old file descriptors: ifstreams cannot read from the device. Can anyone think of a reason?

const char* path = "/dev/disk3";
char        b;

// this works
FILE* f= fopen(path, "rb");
int i = fread(&b, 1, 1, f);     // returns 1, success!

// this does not work
ifstream    cf(path, ios::binary);
cf.read(&b, 1);
bool        good = cf.good();   // returns false, failure.
+3
source share
3 answers

512- . ifstream 1023 , "Invalid argument". , ifstream FILE *. , FILE *, , , .

dtrace . , , , , , , .

+3

, , / ++, .

, ios::in openmode. 27.8.1.6 ios::, , , .

, , , good(), bad(), eof() fail() . , .

+3

I found random problems like this in I / O C ++ for many years. It always seemed to me the opposite step.

I have not tried it, but you can see what Boost has to offer:

http://www.boost.org/doc/libs/1_36_0/libs/iostreams/doc/index.html

0
source

Source: https://habr.com/ru/post/1697923/


All Articles