1. 定义结构体
typedef struct test_cfg {
unsigned char version[4];
unsigned char golden_f0;
unsigned short low_map_freq;
unsigned short high_map_freq;
}test_cfg_t;
txt配置如下:
0x01, // version,V1.1.9.38
0x01,
0x09,
0x26,
0xB4, //golden_f0 170HZ
0x78, //low_map_freq 120HZ
0x00,
0xdc, // high_map_freq 220HZ
0x00,
txt转bin后,将bin文件读入结构体,然后打印结构体各变量接收到的结果如下:
version[0] | 0x01 | 1 | |
version[1] | 0x01 | 1 | |
version[1] | 0x09 | 9 | |
version[1] | 0x26 | 38 | |
golden_f0 | 0xB4 | 180 | |
0x78 | |||
low_map_freq | 0x00 | 56320 | |
0xdc | |||
high_map_freq | 0x00 | 52224 | |
0xcc(该地址没申请,默认值为cc) |
上述结构体定义有问题,需考虑结构体字节对齐,否则将bin文件对结构体会出现错乱问题,一定要注意。
调整结构体结构如下:
typedef struct test_cfg {
unsigned char version[4];
unsigned short low_map_freq;
unsigned short high_map_freq;
unsigned char golden_f0;
}test_cfg_t;
txt配置如下:
0x01, // version,V1.1.9.38
0x01,
0x09,
0x26,
0x78, //low_map_freq 120HZ
0x00,
0xdc, // high_map_freq 220HZ
0x00,
0xB4, //golden_f0 170HZ
结果如下:
具体实现步骤如下:
- 1. 定义接收参数的结构体结构及变量;
- 2. 配置参数配置文件txt文件;
- 3. 按行读取txt文件,通过字符串分割将目标去除保存到新的txt文件;
- 4. 将纯净版txt文件保存成bin文件;
- 5. 将bin文件读入到结构体指针;
- 6. 打印结构体中各成员变量接收到的参数;
main.h
#include<iostream>
#include<fstream>
#include<string>
#include<vector>
using namespace std;
typedef struct test_cfg {
unsigned char version[4];
unsigned short low_map_freq;
unsigned short high_map_freq;
unsigned char golden_f0;
}test_cfg_t;
// String Split
vector<string> split(const string &str, const string &pattern)
{
//const char* convert to char*
char * strc = new char[strlen(str.c_str()) + 1];
strcpy(strc, str.c_str());
vector<string> resultVec;
char* tmpStr = strtok(strc, pattern.c_str());
while (tmpStr != NULL)
{
resultVec.push_back(std::string(tmpStr));
tmpStr = strtok(NULL, pattern.c_str());
}
delete[] strc;
return resultVec;
};
// Read txt by line
int read_txt_lines(const char* path, const char* dst_path)
{
ifstream ifs(path);
ofstream ofs(dst_path);
string line;
int rows = 0;
while (getline(ifs, line))
{
vector<string>str_vector;
str_vector = split(line, ",");
if (!str_vector.empty()) {
ofs << str_vector[0] << '\n';
rows++;
}
}
ifs.close();
ofs.close();
return rows;
}
int read_raw_hex_data(const char* path, int data_length, uint8_t * a) {
FILE* fpRead = NULL;
int ret = 0;
int i = 0;
fopen_s(&fpRead, path, "r");
if (fpRead == NULL)
{
printf("Fail to read raw data file!");
ret = -1;
return ret;
}
for (i = 0; i < data_length; i++)
{
fscanf_s(fpRead, "%x", &a[i]);
}
fclose(fpRead);
return ret;
}
void writeBin(std::string path, char *buf, int size)
{
std::ofstream outfile(path, std::ifstream::binary);
outfile.write((char *)(buf), size);
outfile.close();
}
void read_bin_params(const char* bin_path, test_cfg_t *test_cfg_p)
{
FILE* fp = fopen(bin_path, "rb");
fseek(fp, 0, SEEK_SET);
fread(test_cfg_p, sizeof(test_cfg_t), 1, fp);
fclose(fp);
}
main.cpp
#include<iostream>
#include "main.h"
using namespace std;
int main()
{
const char *path = "C:\\Users\\TongXiaoBin\\Desktop\\c++_test\\test.txt";
const char *dst_path = "C:\\Users\\TongXiaoBin\\Desktop\\c++_test\\test_clean.txt";
const char* bin_path = "C:\\Users\\TongXiaoBin\\Desktop\\c++_test\\test.bin";
// 获得纯净版txt文件
int rows = read_txt_lines(path, dst_path);
// txt转bin文件
uint8_t *data = (uint8_t *)malloc(rows);
read_raw_hex_data(dst_path, rows, data);
writeBin(bin_path, (char*)data, rows);
// 将bin参数读入到算法结构体中
test_cfg_t test_cfg_s;
read_bin_params(bin_path, &test_cfg_s);
for (int i = 0; i < 4; i++) {
cout << (int)test_cfg_s.version[i] << endl;
}
cout<<(int)test_cfg_s.low_map_freq<< endl;
cout<<(int)test_cfg_s.high_map_freq<< endl;
cout << (int)test_cfg_s.golden_f0 << endl;
}