欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > c/c++ >内容正文

c/c++

C++:读写二进制文件到double数组,根据二进制数据量进行读取

发布时间:2025/4/5 c/c++ 54 豆豆
生活随笔 收集整理的这篇文章主要介绍了 C++:读写二进制文件到double数组,根据二进制数据量进行读取 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

代码和数据在git

#include <math.h> #include <fstream> #include <iostream>#include <fstream> #include <iostream> #include <sstream> #include <string>using namespace std;//int readBinFile(std::string& filename, void*& bufPtr, int& pointNum, int pointDim)int main() {// open the file:std::streampos fileSize; //实例化fpos用于表示窄向流中的位置。std::ifstream file("cloud.bin", std::ios::binary);cout<<"file="<<!file<<endl;if (!file) {cout<<"!file"<<endl;return false;}// get its size://把文件的读指针结尾位置向后移前个字节file.seekg(0, std::ios::end);fileSize = file.tellg();//把文件的写指针从文件开头向后移 0 个字节file.seekg(0, std::ios::beg);cout<<fileSize<<endl;int length=fileSize;double* f2 = new double[length];std::ifstream ifs("cloud.bin", std::ios::binary | std::ios::in);ifs.read((char*)f2, sizeof(double) * length);ifs.close();cout<<f2<<endl;/***for (int i = 0; i < length; i++){std::cout<<f2[i]<<std::endl;}***/return 0;}// read the data:

总结

以上是生活随笔为你收集整理的C++:读写二进制文件到double数组,根据二进制数据量进行读取的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。