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
原创粉丝点击