C++时间,文件及字符常用函数总结


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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值