C++时间,文件及字符常用函数总结
来源:互联网 发布:阿里云手机官网 编辑:程序博客网 时间:2024/06/09 20:20
boost获取格式化时间:
#include <boost/date_time/posix_time/posix_time.hpp>#define BOOST_DATE_TIME_SOURCE
std::string strTime = boost::posix_time::to_iso_string(boost::posix_time::second_clock::local_time());int pos = strTime.find('T');strTime.replace(pos,1,std::string(" "));strTime.replace(pos - 2,0,std::string("-"));strTime.replace(pos - 4,0,std::string("-"));strTime.replace(pos + 5,0,std::string(":"));strTime.replace(pos + 8,0,std::string(":"));std::cout << strTime.c_str() << std::endl;
boost 获取时间差:
#include <boost/date_time/posix_time/posix_time.hpp>using namespace boost::posix_time ;
int64_t GetCurrentStamp64(){ boost::posix_time::ptime epoch(boost::gregorian::date(1970, boost::gregorian::Jan, 1)); boost::posix_time::time_duration time_from_epoch = // boost::posix_time::microsec_clock::universal_time() - epoch; boost::posix_time::second_clock::universal_time() - epoch; //return time_from_epoch.total_microseconds(); return time_from_epoch.total_seconds();}
universal_time 可改 local_time
boost 的 文件目录:
#include <boost/filesystem.hpp>list<string> getItemListOfpath(string path){list<string> pathList ;boost::filesystem::path dir2(path); boost::filesystem::directory_iterator end; for (boost::filesystem::directory_iterator pos(dir2); pos != end; pos++) pathList.push_back(pos->path().string()); return pathList ;}
c++获取文件大小
long getFileSize(std::string path_){std::ifstream in(path_);std::streamoff flen = 0 ; if( in.is_open() ) { std::fstream::pos_type cur_pos = in.tellg(); in.seekg( 0L, std::ios::end ); std::fstream::pos_type end_pos = in.tellg(); in.seekg( cur_pos, std::ios::beg ); flen = end_pos; }else{ std::cout<<"no File detected when getting file size"<<std::endl; }return flen;}
c++ 标准库检查文件或目录存在(读取及写入文件内容):
#include <iostream>#include <fstream>using std::fstream ;bool checkPathExist(string path_){fstream _file; bool ret = false ;_file.open(path_.c_str(),std::ios::in);if(!_file){ret = false ;cout<<"no such file or directory "<<path_<<endl;}else{ ret = true ;}return ret ;}
#include <iostream>#include <fstream>using namespace std;list<string> getFileContentSortByLine(const string &path_){list<string> retlist ;ifstream infile;infile.open(path_,ios::in);if(!infile){cout<<"no such file or directory "<<path_<<endl;}else{string tmp;while(!infile.eof()){getline(infile, tmp, '\n');retlist.push_back(tmp);}}infile.close();return retlist ;}string getFileContentString(const string &path_){ifstream in(path_, ios::in);istreambuf_iterator<char> beg(in), end;string strdata(beg, end);in.close();return strdata ;}
bool writeFileContents(const string &path_,const string &content_){bool ret = false ; std::ofstream tmpReport(path_); if(!tmpReport){ std::cout<<"open "<<path_<<" error ,check exists or permission"<<std::endl; }else{ tmpReport << content_ ; ret = true ; } tmpReport.close(); return ret ;}
boost 将string分割
std::vector<string> line_depends ;boost::split(line_depends,cmd_string,boost::is_any_of("\n"));
0 0
- C++时间,文件及字符常用函数总结
- 常用C函数----字符及字符串
- 常用时间函数总结
- 常用时间函数总结
- C/C++常用头文件及函数
- Linux C常用头文件及函数
- 常用C时间函数
- C语言中常用的文件操作函数总结及使用方法演示代码
- Linux精确计时,及常用C时间函数
- Linux精确计时,及常用C时间函数(转)
- php 常用函数总结(数组,字符串,时间,文件操作)
- 基于php常用函数总结(数组,字符串,时间,文件操作)
- 基于php常用函数总结(数组,字符串,时间,文件操作)
- 标准C常用库文件及常用函数
- 字符转换常用函数总结
- C常用字符转换函数
- C 字符/字符串常用函数
- C-常用字符操作函数
- android 的Util工具类
- Lab 相关 ---- 无法读取项目文件 xxx.vcxproj 的解决方法
- uva11404
- Android 开发之 ---- 底层驱动开发(一)
- jre,tomcat配置
- C++时间,文件及字符常用函数总结
- 应用程序跳转
- Android 开发之 ---- 底层驱动开发(二)
- 事件传递机制
- ZTE-297520-double-apn-P-CSCF
- 八、二分查找
- OpenCV读取RTSP视频流
- position属性之相对父元素定位
- bzoj 3073: [Pa2011]Journeys