結合Boost官網
這一章比較簡單,沒有什麼特別重要的需要總結。
直接上習題把:
1、創建一個程序,該程序爲位於應用程序當前工作目錄的上一層目錄中的一個名爲 data.txt
的文件創建一個絕對路徑。 例如,如果該程序從 C:\Program Files\Test
執行,則應顯示 C:\Program Files\data.txt
。
#include <boost/filesystem.hpp>
#include <iostream>
#include <windows.h>
#include <string>
using namespace std;
void main()
{
TCHAR convertTemp[256] = { 0 };
TCHAR Temp[256] = { 0 };
string str = boost::filesystem::current_path().parent_path().string();//獲取上一級目錄
//將char_t * 轉成 wchar_t *類型
MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.length(), convertTemp, 256);
SetCurrentDirectory(convertTemp); //設置當前路徑
GetCurrentDirectory(256, Temp); //獲取當前路徑
fstream file;
file.open("data.txt", ios::out);//創建文件"data.txt"
file.close();
cout << boost::filesystem::current_path() << endl; //打印當前路徑
system("pause");
}