• 数据存入文件


    超过5M时,新建文件,以时间命名文件名

    以下程序是简写部分

    #include <ofstream>

    class
    SignalBoardMsg { public: SignalBoardMsg(); SignalBoardMsg( SignalBoardMsg _in v ) = delete; virtual ~SignalBoardMsg(); private: std::string file_name_with_path_; void CreateFileByTime( std::ofstream & outfile ); void WriteFile( std::ofstream & outfile ); void SetFileNameWithPath( std::string file_name ); std::string GetFileNameWithPath(); unsigned long GetFileSize(); bool MakeDir(std::string file_dir); }; ---------------------------------------------------------------------------------------------------------------------------------- SignalBoardMsg::SignalBoardMsg() : file_name_with_path_("") {} SignalBoardMsg::~SignalBoardMsg() { gps_head_thread_->interrupt(); gps_head_thread_->join(); delete gps_head_thread_; } bool SignalBoardMsg::MakeDir(string file_dir) { string home_value = getenv("HOME"); int flag = mkdir((home_value+file_dir).c_str(), 0777); if (flag == 0) { cout<<"make successfully"<<endl; return true; } else { cout<<"make errorly"<<endl; return false; } } void SignalBoardMsg::SetFileNameWithPath( std::string file_name ) { file_name_with_path_.clear(); string home_value = getenv("HOME"); file_name_with_path_ = home_value + "/signal_board_data/" + file_name + ".csv";//"/signal_board_data/signal_board.csv"; } std::string SignalBoardMsg::GetFileNameWithPath() { return file_name_with_path_; } void SignalBoardMsg::CreateFileByTime( ofstream & outfile ) { time_t time_now; struct tm *tm_time_now; time (&time_now); tm_time_now = localtime(&time_now); char show_time[20]; strftime(show_time, 20, "%Y-%m-%d-%H-%M-%S", tm_time_now); //"%x %X %n%Y-%m-%d%H:%M:%S %nzone: %Z " SetFileNameWithPath(show_time); outfile.open(GetFileNameWithPath().c_str(), ios::out | ios::app); outfile << "时间" << "," << "给定速度X" << "," << "给定速度Y" << "," << "给定速度Z" << "," << "反馈速度X" << "," << "反馈速度Y" << "," << "反馈速度Z" << "," << "反馈位姿X" << "," << "反馈位姿Y" << "," << "反馈位姿Z" << "," << "反馈里程X" << "," << "反馈里程Y" << "," << "反馈里程Z" << "," << "陀螺仪X" << "," << "陀螺仪Y" << "," << "陀螺仪Z" << "," << "错误码0" << "," << "错误码1" << "," << "错误码2" << "," << "错误码3" << "," << "错误码4" << "," << "错误码5" << std::endl; } void SignalBoardMsg::WriteFile( ofstream & outfile ) { outfile << s_netpack_.TimeStamp << "," << s_netpack_.SetRobot.Vel.X << "," << s_netpack_.SetRobot.Vel.Y << "," << s_netpack_.SetRobot.Vel.Z << "," << s_netpack_.GetRobot.Vel.X << "," << s_netpack_.GetRobot.Vel.Y << "," << s_netpack_.GetRobot.Vel.Z << "," << s_netpack_.GetRobot.Pos.X << "," << s_netpack_.GetRobot.Pos.Y << "," << s_netpack_.GetRobot.Pos.Z << "," << s_netpack_.GetRobot.Odom.X << "," << s_netpack_.GetRobot.Odom.Y << "," << s_netpack_.GetRobot.Odom.Z << "," << s_netpack_.Gyro.X << "," << s_netpack_.Gyro.Y << "," << s_netpack_.Gyro.Z << "," << s_netpack_.ErrorMsg[0].All << "," << s_netpack_.ErrorMsg[1].All << "," << s_netpack_.ErrorMsg[2].All << "," << s_netpack_.ErrorMsg[3].All << "," << s_netpack_.ErrorMsg[4].All << "," << s_netpack_.ErrorMsg[5].All << std::endl; } unsigned long SignalBoardMsg::GetFileSize() { struct stat buf; if (stat(GetFileNameWithPath().c_str(), &buf)<0) { std::cout << "----------get file size wrong" << std::endl; return 0; } else//bytes=8bit,1024*1024=1Mb=1048576bytes//5M=5242880 { // std::cout << "----------get file size: " << (unsigned long)buf.st_size << std::endl; return (unsigned long)buf.st_size; } } void SignalBoardMsg::Run() { MakeDir("/signal_board_data"); ofstream outfile; CreateFileByTime(outfile); ros::Rate rate ( 50 ); while ( ros::ok() ) { WriteFile(outfile); unsigned long file_size = GetFileSize(); if ( file_size > 5242880 )//5Mb { outfile.close(); CreateFileByTime(outfile); } } }
  • 相关阅读:
    列表 list
    flask实现json数据处理、学生信息表格展示和jinjia2的用法
    flask实现用户登录和上传文件
    移动端APP测试
    charles-截取移动端请求-设置代理
    charles-过滤网络请求方法
    badboy的录制和jmeter的使用
    jmeter之关联
    jmeter集合点
    jmeter之检查点
  • 原文地址:https://www.cnblogs.com/cj2014/p/6838322.html
Copyright © 2020-2023  润新知