gitee地址:
https://gitee.com/open_code_kevin_the_chinese/file_operation
文件操作
功能描述
保存数据至文件
在指定目录下创建文件,并可将数据按行保存至文件,每一行的数据数量与标签数量相同,格式如下:
tag1 tag2 tag3 tag4
data11 data21 data31 data41
data12 data22 data32 data42
加载文件
加载文件,按照保存的格式,分别将标签和数据内容赋值给对应的变量。
用户使用接口文件
include/saveLoadFileOperator.h
将数据保存至文件的使用步骤
设置将文件保存的路径:
void SetFilePath(const std::string& path);
设定首行标签序列的内容
void SetDataTagsNames(const std::vectorstd::string& names);
创建文件,可选择文件名字后面是否带时间信息:
创建文件名不含时间信息
void CreateNewFileGivenName(const std::string& fileName);
创建的文件名后面追加时间信息
void CreateNewFileGivenNameWithTime(const std::string& fileName);
按行保存数据至文件:
void OutputProcessing(const std::vector& data);
关闭文件
void Close() {ofs_.close();};
加载文件至变量中的步骤
设置被读取文件的存放路径:
void SetFilePath(const std::string& path);
指定读取的文件名,将标签和数据存放到对应变量中。变量分全都浮点型和字符串型两种。如果变量中有不同类型的变量,建议采用字符串加载,然后在后一级的模块中再进一步做数据转换。
bool LoadFile(const std::string& fileName, std::vectorstd::string& tags,
std::vector<std::vector>& tableData);
bool LoadFileAsString(const std::string& fileName, std::vectorstd::string& tags,
std::vector<std::vectorstd::string>& tableData);
操作过程
编译
mkdir build & cd build
cmake … -DWITH_TEST=ON
make
运行
数据保存的测试demo
首先在工程目录下常见data文件夹,然后在工程目录下执行:
./test/demo_save_data
运行完毕后,会看到data文件夹下产生了数据文件。
数据加载的测试demo
在工程目录下执行执行如下命令,注意后面传递的参数时实际的文件名称
./test/demo_load_data sin_cos_2024-07-28-12-01.csv
可视化数据
在工程目录下执行执行如下命令,注意要修改py文件中的文件名称与实际的名称一致:
python3 ./test/plot.py
可以看到如下的绘制图片:
示例图片 如果在使用过程时缺少什么库,就根据提示安装对应的库。