如果我需要从 ifstream 读取 int
int myInt = 0;
fileStream.read(reinterpret_cast<char*>(&myInt), sizeof(int));
正在使用 reinterpret_cast<char*>
实现该目标的正确方法?
最佳答案
is using reinterpret_cast correct way to accomplish that?
是的。更喜欢 C++ 风格的转换,而不是 C 风格的转换。
正如评论中所建议的,使用 read method function 的更好方法是:
int myInt = 0;
fileStream.read(reinterpret_cast<char*>(&myInt), sizeof(myInt));
关于c++ - 将 int 地址转换为 char 指针的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8576415/