将wchar_t数组/字符串拆分为两个不同的数组/字符串

时间:2015-10-13 01:19:11

标签: c++ string split

我之前搜索过,并没有真正找到关于这个主题的简洁答案,我想用简单的术语是这样的:

拖放功能为我提供了一个带文件名的目录。

C:\Users\chaos\Desktop\Game Launcher V1.0.exe

我需要将这一个字符串分成两个不同的字符串,以便它可以执行此操作

Directory: C:\Users\chaos\Desktop\

FileName: Game Launcher V1.0.exe

我认为有一个循环可以做到这一点,所以我想知道你对这种情况的意见是什么,我很乐意听到它。谢谢:))

我在Windows上使用Visual Studio 2015.(UTF16)

1 个答案:

答案 0 :(得分:0)

由于您使用的是Windows,请查看PathRemoveFileSpec()PathStripPath()PathFindFileName()函数,例如:

LPTSTR szFullFilename = ...; // value from drag&drop
int iLength = lstrlen(szFullFilename);

LPTSTR szDirectory = new TCHAR[iLength+1];
lstrcpy(szDirectory, szFullFilename);
PathRemoveFileSpec(szDirectory);

LPTSTR szFileName = new TCHAR[iLength+1];
lstrcpy(szFileName, szFullFilename);
PathStripPath(szFileName);

// use szDirectory and szFileName as needed...

delete[] szDirectory;
delete[] szFileName;

LPTSTR szFullFilename = ...; // value from drag&drop
int iLength = lstrlen(szFullFilename);

LPTSTR szDirectory = new TCHAR[iLength];
lstrcpy(szDirectory, szFullFilename);

LPTSTR szFileName = PathFindFileName(szDirectory);
if (szFileName != szDirectory)
    *(szFileName-1) = 0;

// use szDirectory and szFileName as needed...

delete[] szDirectory;
相关问题