本总结只是针对于boost1.54.0版本库:
1.判断文件是否存在:
#include <boost/filesystem.hpp>
std::string m_parent_dir;
m_parent_dir.append(“C://user.bat”);
boost::filesystem::exists(m_parent_dir)
2.创建文件路径或者文件
#include <boost/filesystem.hpp>
boost::system::error_code ec;
std::string m_parent_dir;
m_parent_dir.append(“C://user.bat”);
boost::filesystem::create_directories(m_parent_dir, ec);
//ec这里表示错误码
3.数据类型和字符串之间的转化
#include <boost/lexical_cast.hpp>
std::string str_tern;
int index = 10;
str_tern = "_ID=";
str_tern += boost::lexical_cast<std::string>(index);
3.函数适配
它支持函数、函数对象、函数指针和成员函数指针。它可以绑定任何参数到一个具体的值或者函数到预定义好的位置。常与boost::thread一起使用。
#include <boost/bind.hpp>
boost::thread threads(boost::bind(函数名, 参数1,参数2));
对自由方法来说,直接boost::bind(函数名, 参数1,参数2,...);
对类方法来说,直接boost::bind(&类名::方法名,类实例指针,参数1,参数2)
1 void f(int a, int b)
2 {
3 cout << a + b << endl;
4 }
5 void g(int a, int b, int c)
6 {
7 cout << a + b + c << endl;;
8 }
1 int a = 10;
2 boost::bind(f, _1, 5)(a);
3 int x(10),y(20),z(30);
4 boost::bind(g,_1,_2,_3)(x, y, z);
结果是:15 60
4.创建线程
#include <boost/thread.hpp>
void wait(int seconds) { boost::this_thread::sleep(boost::posix_time::seconds(seconds)); //休眠1秒} void thread() { for (int i = 0; i < 5; ++i) { wait(1); std::cout << i << std::endl; } } int main() { boost::thread t(thread); t.join(); // 为了防止程序终止 ,阻塞调用,它可以暂停当前线程,直到调用 join() 的线 程运行结束。 这就使得main() 函数一直会等待到 thread() 运行结束。
t.detach() //与join()二选一,作用是把线程从主线程分离,}
5.字符串格式化
接下来简单说明一下format的用法。估计聪明人都猜到了,在格式化字符串中,“%1%”(不带引号,后称占位符)表示后面跟的第一个参数,“%2%”则表示第二个,以此类推——注意:占位符是从1开始计数,兄弟们别记错了。后面的“%”(当然也不带引号)操作符的意思么……这是format类(千万别把它当作是个函数)重载的操作符,用来替换格式化字符串中的占位符。
#include <boost/format.hpp>
boost::format fmt("%1%号计算机需要网管帮助。");
fmt % host_name.c_str();
auto show_str = fmt.str();
boost::format formater("%s");
formater % "输出内容";
std::string s = formater.str();
cout << s << endl;
5.获取debug文件夹下文件路径
char * filePath=new char[MAX_PATH];
GetModuleFileNameA(NULL,filePath ,MAX_PATH);
std_string updateexe_path;
updateexe_path.append(filePath);
updateexe_path.append("\\..\\update_register.exe");
6.启动线程方法:
#include <process.h>
void test();
void main()
{
_beginthread(test,0,NULL);
}
7.获取当前系统时间方法
包含头文件路径 D:\Projects\KHULibs\mwf
#include <KHAboutString.h>
获取今天时间的开始:eg:2014/7/8 00:00:00
std_string daytime = KHAboutString::DataTimeString::get_yestorday().begin;
8.RC4加密方法
#include <MRC4.h>
包含头文件路径D:\Projects\KHULibs\mwf
std::string reg_info;
Int reg_len = reg_info.size();
MRC4::DoCrypt((unsigned char*)reg_info.c_str(), reg_len,
(unsigned char *)("kungho2014"));
9.启用指定路径软件方法
char * filePath=new char[MAX_PATH];
GetModuleFileNameA(NULL,filePath ,MAX_PATH);
std_string updateexe_path;
updateexe_path.append(filePath);
updateexe_path.append("\\..\\update_register.exe");
ShellExecuteA(NULL, "open", updateexe_path.c_str(), NULL, NULL, SW_SHOWNORMAL );
10.获取一个文件夹下所有文件:
在项目中包含D:\feiQ_项目相关\password_tool\Repeater\DiskScan.h
D:\feiQ_项目相关\password_tool\Repeater\DiskScan.cpp
#include "DiskScan.h"
回调函数:
BOOL ScanBreadthCallBack(IFile * pFile, WPARAM wParams, LPARAM lParam)
{
auto file_music = (std::vector<std::string >*)wParams;
if (pFile->GetType() == IFile::type_dir)
{
for (auto it = pFile->DirData().begin(); it != pFile->DirData().end(); it++)
{
if ((*it)->GetType() == IFile::type_file)
{
file_music->push_back((*it)->GetName());
}
}
}
return TRUE;
}
CDirScan cds(CDirScan::SingleThread_Scan);
std::vector<std::string> file_vt;
std_string* dir_path = new std_string(buf_path);
cds.ScanBreadth(dir_path, 2, (WPARAM)&file_vt, NULL, ScanBreadthCallBack);
for (auto it = file_vt.begin(); it != file_vt.end(); it++)
{
std::string file_name = *it; //循环取文件名
}
11.创建选择文件夹对话框(duilib)
#include <atldlgs.h>
CFolderDialog Filedlg_Main;
std_string buf_path;
if (Filedlg_Main.DoModal(m_hWnd) == IDOK)
{
buf_path = Filedlg_Main.GetFolderPath();
}
std::string filelist_path;
filelist_path = buf_path; //filelist_path为文件夹路径
12.创建打开文件对话框
OPENFILENAMEA ofn = { 0 };
char strFileName[MAX_PATH] = "";
memset(&ofn, 0, sizeof(OPENFILENAME));
memset(strFileName, 0, sizeof(char)*MAX_PATH);
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.lpstrFilter = "配置(*.json)";
ofn.lpstrFile = strFileName;
ofn.nFilterIndex = 1;
ofn.nMaxFile = MAX_PATH;
ofn.Flags = OFN_FILEMUSTEXIST;
BOOL ret = GetOpenFileNameA(&ofn);
if (ret)
{
char* json_name = ofn.lpstrFile;//json_name为打开文件的完整路径
}
13.创建保存文件对话框
CString filePath;
LPCTSTR szFilter = _T("All Files(*.*)|*.*||");
CFileDialog dlg(FALSE, _T(".json"), _T("pwd_json.json"), OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, szFilter, NULL);
char *file_path = new char(MAX_PATH);
if (dlg.DoModal(m_hWnd) == IDOK)
{
dlg.GetFolderPath(CA2T(file_path), MAX_PATH);
std::string filelist_path;
filelist_path.append(file_path);
}
14.获取任务管理器进程列表
#include <KHAboutProcess.h>
auto process_list = KHAboutProcess::GetProcessLst();
for (auto it = process_list.begin(); it != process_list.end(); ++it)
{
WCHAR * process_name = it->second.szExeFile;
std_wstring process_name = it->second.szExeFile;
}
15.根据路径获取文件名
#include <boost/filesystem.hpp>
boost::filesystem::path music_path = CA2T(it->c_str());
auto file_name = music_path.filename().string();
16.让程序自动生成dup文件
把CrashHelper.dll放在debug目录下,然后在程序入口出添加代码:
LoadLibraryA("CrashHelper.dll");
17.网络通信转码方式
u8_toutf8 u8_toutf16
#include "utf8.h" D:\magic_code_src\package\include\ulib
#pragma comment(lib, "ulib.lib") D:\Projects\KHULibs\ulib\lib\StaticDbg
#include <KHAboutString.h>
发送网络通信数据,中文编码
std_wstring barname(“网吧名字”);
std_string bar_name=u8_toutf8(barname);
如果是post发送,不需要使用url_decode再进行编码
如果是get发送,上传是需要先进行utf8编码,再使用url_encode进行编码,
下载的时候需要使用url_decode进行解码,再使用u8_toutf16进行解码。
std::string url_date = KHAboutString::Code::url_decode(voice_text_info);
std_wstring wurl_date=u8_toutf16(url_date);