Hello Mat

 找回密码
 立即注册
查看: 793|回复: 0

C++读取C#的byte[]文件

[复制链接]

1298

主题

1524

帖子

114

金钱

管理员

Rank: 9Rank: 9Rank: 9

积分
22653
发表于 2024-3-31 12:22:29 | 显示全部楼层 |阅读模式
  1. // 指定要读取的文件路径和名称  
  2.         const char* filePath = "D:\\2-LearningCode\\902-ASR\\Github\\Model\\output.bin";
  3.         // 使用ifstream以二进制模式打开文件  
  4.         //std::ifstream file(filePath, std::ios::binary);
  5.         std::ifstream file(filePath, std::ios::binary);
  6.         // 移动到文件末尾以获取文件大小  
  7.         file.seekg(0, std::ios::end);
  8.         std::streamsize fileSize = file.tellg();
  9.         file.seekg(0, std::ios::beg);
  10.         // 分配足够的空间来存储文件内容  
  11.         std::vector<unsigned char> buffer(fileSize);
  12.         // 读取文件内容到vector<unsigned char>中  
  13.         if (!file.read(reinterpret_cast<char*>(buffer.data()), fileSize))
  14.         {
  15.                 std::cerr << "读取文件时出错" << std::endl;
  16.                 return 1;
  17.         }
  18.         // 输出读取到的数据  
  19.         //for (const auto& b : buffer)
  20.         //{
  21.         //        std::cout << std::hex << static_cast<int>(b) << " "; // 以十六进制格式输出  
  22.         //}
  23.         //std::cout << std::endl;
  24.         file.close(); // 关闭文件
  25.         // 此时,buffer包含文件的内容,可以像使用unsigned char*一样使用它  
  26.         unsigned char* data = buffer.data();
  27.         std::cout << fileSize << std::endl;

  28.         char* output1 = PredictASRBytes_GPU(data, 179778, 16000, 0.02, 0.01);
  29.         std::cout << output1 << std::endl;
复制代码

在C#中,使用File.WriteAllBytes方法写入一个二进制文件;File.ReadAllBytes方法来读取整个文件内容到一个字节数组中。

算法QQ  3283892722
群智能算法链接http://halcom.cn/forum.php?mod=forumdisplay&fid=73
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Python|Opencv|MATLAB|Halcom.cn ( 蜀ICP备16027072号 )

GMT+8, 2024-5-17 15:25 , Processed in 0.218869 second(s), 22 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表