#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
/**
* 读取文件数据到vector中
* @param file_name 文件名
*/
std::vector<float> read_file(const std::string &file_name)
{
int begin, end;
std::ifstream file(file_name, std::ios::in | std::ios::binary);
if (!file)
{
std::cout << "input file " << file_name << " not found";
exit(-1);
}
begin = file.tellg();
file.seekg(0, std::ios::end);
end = file.tellg();
size_t len = end - begin;
std::cout << "文件len: " << len <<endl;
std::vector<float> img_bytes(len / 4);
file.seekg(0, std::ios::beg);
file.read(reinterpret_cast<char *>(img_bytes.data()), len);
return img_bytes;
}
int main()
{
std::string filename;
std::cout << "请输入bin文件名或路径: ";
std::cin >> filename;
std::vector<float> data = read_file(filename);
for (float f : data)
{
std::cout << "value:" << f << endl;
}
return 0;
}
C++读取float数据文件内容
于 2022-03-10 17:50:13 首次发布