C++ vector与数组转换写入/读出文件方式
作者:Android系统攻城狮
这篇文章主要介绍了C++ vector与数组转换写入/读出文件方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
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 | 变量 | 说明 |
---|---|---|
FileStatistics | signature | DWORD , 信号量 |
statisticsSize | DWORD , sizeof(FileStatistics) | |
applicationId | BYTE, applicat ion ID(usually CANoe) | |
applicationMajor | BYTE , application major number (usually 0) | |
applicationMinor | BYTE , application minor number (usually 0) | |
applicationBuild | BYTE , application build number (usually 0) | |
apiMajor | BYTE , BL API major number (e.g. 3) | |
apiMinor | BYTE , BL API minor number (e.g. 9) | |
apiBuild | BYTE , BL API major number (e.g. 6) | |
apiPatch | BYTE , BL API patch number (e.g. 3) | |
fileSize | ULONGLONG ,文件大小 以字节记 | |
uncompressedFileSize | ULONGLONG ,解压缩文件大小 | |
objectCount | DWORD ,number of objects | |
objectsRead | DWORD ,number of objects read | |
measurementStartTime | SYSTEMTIME ,测量开始时间 | |
lastObjectTime | SYSTEMTIME ,最后一个对象的时间 | |
reserved[18] | DWORD | |
LogContainer | objectFlags | DWORD,object flags (usually BL_OBJ_FLAG_TIME_ONE_NANS) |
reserved | WORD | |
objectVersion | WORD, object specific version (usually 0) | |
uncompressedFileSize | ULONGLONG,解压文件大小 | |
compressedFileSize | std::streamsize,压缩文件大小 | |
compressedFile | char *,压缩文件内容 ,存储压缩后的can object |
can message 内容
TYPE | 变量 | 说明 |
---|---|---|
ObjectHeaderBase | signature | DWORD , 目标信号 |
headerSize | WORD ,sizeof object header ,取决于header的类型 | |
headerVersion | WORD ,object header 的类型(objectheader 有两种版本,以下只解释version 1) | |
objectSize | DWORD ,can object的大小 | |
objectType | DWORD ,can object的类型 | |
ObjectHeader | objectFlags | DWORD ,object 时间戳的单位 :10微妙或1ns |
reserved‘[’2] | BYTE | |
objectVersion | WORD ,object specific version,必须设为0 | |
objectType | DWORD ,can object的类型 | |
objectTimeStamp | ULONGLONG,can object在 objectFlags给出单位上的时间戳 | |
CanMessage | channel | WORD ,通道数 |
flags | BYTE , Bit 0: TX 、Bit 5: NERR、Bit 6: WU、 Bit 7: RTR | |
dlc | BYTE | |
id | DWORD | |
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 .
这些仅为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。