亲宝软件园·资讯

展开

C++ vector与数组转换写入/读出文件方式

Android系统攻城狮 人气:0

vector与数组转换写入/读出文件

#include <iostream>
#include <fstream>
#include <string.h>
#include <vector>
using namespace std;
 
template <class T> int compute_array_len(T &arr){
  int len = sizeof(arr)/sizeof(arr[0]);
  return len;
}
 
int main(){
  float f_arr[]={0.0353853,0.0463009,-0.00107202,-0.0329376,-0.0352677,-0.0548481,-0.0023105};
  int num = compute_array_len(f_arr);
  int len = sizeof(float) * num;
 
  vector<float> vec;
  {//1.把float数组插入到vector<float>
    for(int i = 0; i < num; i++)
      vec.push_back(f_arr[i]);
  }
  {//2.auto遍历
    cout <<"v: ";
    for(auto v : vec)
      cout << v << " ";
    cout << endl;
  }
  {//3.iterator迭代器遍历
    cout << "itr: ";
    for(vector<float>::iterator itr = vec.begin(); itr != vec.end(); itr++)
      cout << *itr << " ";
    cout << endl;
  }
  
  cout << "vec.size = " << vec.size() << endl;
  float da[vec.size()] = {0};
  {
    //4.把vector<float>遍历赋值给数组,然后写入文件
    for(int i = 0; i < vec.size(); i++){
      da[i] = vec[i];
      cout << "da["<< i << "] = " << da[i] << " ";
    }
    cout << endl;
  }
 
  float b[num];
  //1.write file
  ofstream ouF;
  ouF.open("test.txt", ios::binary);
  ouF.write((const char*)da, len);//写入文件
  ouF.close();
 
  //2.read file
  ifstream inF;
  inF.open("test.txt", ios::binary);
  inF.read((char *)b, len);
  inF.close();
 
  for(int i = 0; i < num; i++){
    cout<<"read file: " << b[i] << " ";
  }
  cout << endl;
  return 0;
}

vector BLF 文件读写

1 BLF文件格式

文件头部包含内容

TYPE变量说明
FileStatisticssignatureDWORD , 信号量
 statisticsSizeDWORD , sizeof(FileStatistics)
 applicationIdBYTE, applicat ion ID(usually CANoe)
 applicationMajorBYTE , application major number (usually 0)
 applicationMinorBYTE , application minor number (usually 0)
 applicationBuildBYTE , application build number (usually 0)
 apiMajorBYTE , BL API major number (e.g. 3)
 apiMinorBYTE , BL API minor number (e.g. 9)
 apiBuildBYTE , BL API major number (e.g. 6)
 apiPatchBYTE , BL API patch number (e.g. 3)
 fileSizeULONGLONG ,文件大小 以字节记
 uncompressedFileSizeULONGLONG ,解压缩文件大小
 objectCountDWORD ,number of objects
 objectsReadDWORD ,number of objects read
 measurementStartTimeSYSTEMTIME ,测量开始时间
 lastObjectTimeSYSTEMTIME ,最后一个对象的时间
 reserved[18]DWORD
LogContainerobjectFlagsDWORD,object flags (usually BL_OBJ_FLAG_TIME_ONE_NANS)
 reservedWORD
 objectVersionWORD, object specific version (usually 0)
 uncompressedFileSizeULONGLONG,解压文件大小
 compressedFileSizestd::streamsize,压缩文件大小
 compressedFilechar *,压缩文件内容 ,存储压缩后的can object

can message 内容

TYPE变量说明
ObjectHeaderBasesignatureDWORD , 目标信号
 headerSizeWORD ,sizeof object header ,取决于header的类型
 headerVersionWORD ,object header 的类型(objectheader 有两种版本,以下只解释version 1)
 objectSizeDWORD ,can object的大小
 objectTypeDWORD ,can object的类型
ObjectHeaderobjectFlagsDWORD ,object 时间戳的单位 :10微妙或1ns
 reserved‘[’2]BYTE
 objectVersionWORD ,object specific version,必须设为0
 objectTypeDWORD ,can object的类型
 objectTimeStampULONGLONG,can object在 objectFlags给出单位上的时间戳
CanMessagechannelWORD ,通道数
 flagsBYTE , Bit 0: TX 、Bit 5: NERR、Bit 6: WU、 Bit 7: RTR
 dlcBYTE
 idDWORD
 data’['8]BYTE

2 BLF文件写入流程

3 注意事项

注意各帧报文timstamp与filestatistics上lastObjectTime参数的对应设置

blf文件中measurementStartTime 与lastObjectTime的时间间隔不宜过短 ,以小于1ms为例。

以上两种情况均容易产生在canoe解析时发生 Warning: The configured time interval is outside the logging file’s scope .

这些仅为个人经验,希望能给大家一个参考,也希望大家多多支持。

加载全部内容

相关教程
猜你喜欢
用户评论