改cpp[0] dat文件存的是int16的数,C++如何读取?

我有一个dat格式的文件,其按照int16的格式存储着数据。而且是一个包含2048800400的数组。第一个任务,便是从中取出数据,并reshape想要的形状。
首先,先要能从dat文件中取出一个int16数。

#include <iostream>
#include <fstream>
//#include <opencv2/opencv.hpp>

using namespace std;

int main(){
    
  std::string data_name="RAW-2048-001-400-002-400-1.dat";
  std::ifstream file(data_name,ios::binary);
  	if (!file.good())
	  std::cerr << "文件无法打开,请确定文件是否可用!" << std::endl;
	size_t size = 0;
	file.seekg(0, file.end);	// 将读指针从文件末尾开始移动0个字节
	size = file.tellg();	// 返回读指针的位置,此时读指针的位置就是文件的字节数
	file.seekg(0, file.beg);	// 将读指针从文件开头开始移动0个字节
	int16_t data;
	file.read((char*)&data,sizeof(data));
	file.close();
  cout<<data<<endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值