Difference between static_cast and (char*)

后端 未结 5 1805
天命终不由人
天命终不由人 2020-12-20 18:43

this is my first question :)

I have one pile file, and I have open it like shown below ;

ifstream in ( filename,  ios :: binary | ios :: in ) 
         


        
5条回答
  •  无人及你
    2020-12-20 19:06

    You will typically get these errors during binary file I/O using ifstream or ofstream or fstream. Problem is that these streams have methods that take const char* while what you have is array of some other type. You want to write your array as binary bits in to the file.

    The traditional way of doing this is using old-style cast (char*) which basically just says that whatever pointer I've treat it as (char*). The old style casts are discouraged by pedantic/strict mode. To get rid of those warnings, the C++11 equivalent is reinterpret_cast.

    I would say, if you are doing binary file I/O then you already know things may or may not be portable depending on how you save the file in one OS and read in another OS. That's whole another issue, however, don't get scared by reinterpret_cast because that's what you have to do if you want to write bytes to file.

提交回复
热议问题