人们会怎么做呢?另外,有没有简单的方法来做到这一点?使用像 Boost 之类的库?
最佳答案
写出 int 的 DataOutputStream 写出一个 4 字节的 int,高字节在前。读入 char*,重新解释,如果需要转换字节顺序,请使用 ntohl。
ifstream is;
is.open ("test.txt", ios::binary );
char* pBuffer = new char[4];
is.read (pBuffer, 4);
is.close();
int* pInt = reinterpret_cast<int*>(pBuffer);
int myInt = ntohl(*pInt); // This is only required if you are on a little endian box
delete [] pBuffer;
关于java - 从 C++ 中 java 的 writeInt 方法写入的文件中读取一个 int?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1169455/