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:
posted @   luoganttcc  阅读(63)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· 字符编码:从基础到乱码解决
· Open-Sora 2.0 重磅开源!
点击右上角即可分享
微信分享提示