java - 从 C++ 中 java 的 writeInt 方法写入的文件中读取一个 int?

标签 java c++ int iostream

人们会怎么做呢?另外,有没有简单的方法来做到这一点?使用像 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/

相关文章:

java - 将 libGDX 中的图像保存到 iOS Gallery

c++ - 创建一个 ID 静态成员变量来跟踪类 ID

c++ - 扩展非类型参数包以定义带有非类型模板参数的内部类模板是否合法?

C 编程 printf ("%d", 50.2);

arrays - Swift 将 String 数组转换为 Int 数组

java - 将字符串拆分为 3 部分并转换为 int

java - 在异常 : Cannot find operation isServiced 的 tomcat 中部署

java - 为什么资源包没有加载到java支持的webscript中

java - 间歇性 "java.sql.SQLException: org.apache.commons.dbcp.DelegatingStatement is closed."

c++ - {} 在这些 C++ 函数中意味着什么?