cpp-filesystem

路径处理

doc

#include <filesystem>
using namespace std::filesystem;

// 这个坑货,快捷方式启动的时候,路径不对了
fs::path p = fs::current_path();

分割路径

_splitpath_s

BOOL SetCurrentPathToModulePath(HMODULE hModule)
{
TCHAR szPath[MAX_PATH];
if(::GetModuleFileName(hModule, szPath, MAX_PATH))
{
TCHAR drive[MAX_PATH], dir[MAX_PATH], fname[MAX_PATH], ext[MAX_PATH];
_tsplitpath_s(szPath,drive, MAX_PATH,dir, MAX_PATH,fname, MAX_PATH,ext, MAX_PATH);
lstrcpy(szPath, drive);
lstrcat(szPath, dir);

return ::SetCurrentDirectory(szPath);
}

return FALSE;
}

路径是否存在

if (FALSE == PathFileExists( (m_configPath + _T("logs")).c_str))
{
// directory not exist
DWORD dwErrCode = GetLastError();
}

crate folder

//create a path with title name
if ( FALSE == CreateDirectory(tmpSrcPath,NULL) )
{
AfxMessageBox(TEXT("Create directory failed"));
return;
}

文件相关处理,copy,move,delete

CopyFile(),DeleteFile(),MoveFile()


#define OP_RENAME 0
#define OP_COPY 1
#define OP_DELETE 2
#define OP_MOVE 3
/*
rename file or directory
@param iType [in] 0 rename, 1 copy,2 delete,3 move
0 success
not zero failed.
*/
int filePathProc(const wchar_t *pFrom, const wchar_t *pTo, int iType)
{
SHFILEOPSTRUCT FileOp = { 0 };
wchar_t src[MAX_PATH] = { '\0' };
wchar_t dst[MAX_PATH] = { '\0' };
if ( NULL == pTo )
{
FileOp.pTo = NULL;
} else
{
_tcscpy(dst, pTo);
FileOp.pTo = dst;
}
if ( NULL == pFrom )
{
return 1;
}
_tcscpy(src, pFrom);

switch (iType)
{
case 0:
FileOp.wFunc = FO_RENAME;
break;
case 1:
FileOp.wFunc = FO_COPY;
break;
case 2:
FileOp.wFunc = FO_DELETE;
FileOp.pTo = NULL;
break;
case 3:
FileOp.wFunc = FO_MOVE;
default:
break;
}
FileOp.fFlags = FOF_NOCONFIRMATION | FOF_ALLOWUNDO | FOF_NOCONFIRMMKDIR; //不出现确认对话框
FileOp.pFrom = src;

return SHFileOperation(&FileOp);
}

int bRet = filePathProc((utf16Path + TEXT("123")).c_str(), (utf16Path + TEXT("456")).c_str(), OP_RENAME);
_ASSERT(false == bRet);

string strUtf8;
UnicodeConverter::toUTF8(utf16Path + TEXT("456"), strUtf8);
File f1( strUtf8 );
_ASSERT(true == f1.exists());

bRet = filePathProc((utf16Path + TEXT("456")).c_str(), (utf16Path + TEXT("123")).c_str(), OP_RENAME);
_ASSERT(false == bRet);
UnicodeConverter::toUTF8(utf16Path + TEXT("123"), strUtf8);
f1 = (strUtf8);
_ASSERT(true == f1.exists());

bRet = filePathProc((utf16Path + TEXT("123")).c_str(), (utf16Path + TEXT("1233")).c_str(), OP_COPY);
_ASSERT(false == bRet);
UnicodeConverter::toUTF8(utf16Path + TEXT("1233"), strUtf8);
f1 = (strUtf8);
_ASSERT(true == f1.exists());

bRet = filePathProc((utf16Path + TEXT("123")).c_str(), (utf16Path + TEXT("1234")).c_str(), OP_COPY);
_ASSERT(false == bRet);
UnicodeConverter::toUTF8(utf16Path + TEXT("1234"), strUtf8);
f1 = (strUtf8);
_ASSERT(true == f1.exists());

bRet = filePathProc((utf16Path + TEXT("1234")).c_str(), (utf16Path + TEXT("1233")).c_str(), OP_MOVE);
_ASSERT(false == bRet);
UnicodeConverter::toUTF8(utf16Path + TEXT("1233\\1234"), strUtf8);
f1 = (strUtf8);
_ASSERT(true == f1.exists());

bRet = filePathProc((utf16Path + TEXT("1233")).c_str(), NULL, OP_DELETE);
_ASSERT(false == bRet);
UnicodeConverter::toUTF8(utf16Path + TEXT("123"), strUtf8);
f1 = (strUtf8);
_ASSERT(true == f1.exists());

读写文件

//读取文件验证
FILE * fp = NULL;
errno_t ret = fopen_s(&fp,w2s(strAppPath).c_str(), "rb");
if (ret)
{
OutputDebugString(_T("open active file failed."));
m_flag = 1;
return TRUE;
}
else
{
uint32_t length = 0;
size_t ret = fread(&length, 1, 4, fp);
if (ret != 4)
{
OutputDebugString(_T("read active file failed."));
m_flag = 1;
fclose(fp);
return TRUE;
}

char* pbuf = new char[length];
memset(pbuf, 0, length);
ret = fread(pbuf, 1, length, fp);
fclose(fp);
...
delete[] pbuf;
}
// 写入文件

FILE * fp = NULL;
errno_t ret = fopen_s(&fp, w2s(strAppPath).c_str(), "wb");
if (ret)
{
OutputDebugString(_T("open active file for write failed."));
AfxMessageBox(_T("保存激活信息失败,请确认程序目录可写。"));
return;
}
else {
strInfo = w2s(str.GetBuffer());
int iRet = fwrite(strInfo.c_str(), sizeof(char), strInfo.size(), fp);
if (iRet != strInfo.size())
{
OutputDebugString(_T("write register info failed."));
AfxMessageBox(_T("保存激活信息失败,请确认程序目录可写。。"));
}
}
fclose(fp);