hkB*_*sai 5 c++ winapi long-filenames string-parsing
我正在通过使用库函数_wsplitpath_s()来解析路径以与 Win32 API 函数一起使用。
根据此 MSDN 文档,长路径(长于MAX_PATH字符)必须以前缀\\?\. 但是,当我将它附加到完整路径字符串时,_wsplitpath_s()无法正确解析它。
示例代码:
std::wstring FullPath = L"\\\\?\\K:\\A Directory\\Another Directory\\File Name.After Period.extension";
std::wstring Drive, Directory, FileName, Extension;
Drive.resize (FullPath.size());
Directory.resize (FullPath.size());
FileName.resize (FullPath.size());
Extension.resize (FullPath.size());
errno_t Error = _wsplitpath_s( FullPath.c_str(),
&Drive[0],
Drive.size(),
&Directory[0],
Directory.size(),
&FileName[0],
FileName.size(),
&Extension[0],
Extension.size());
std::wcout << L"Full path to be splitted : " << FullPath.c_str() << std::endl;
std::wcout << L"Drive : " << Drive.c_str() << std::endl;
std::wcout << L"Directory : " << Directory.c_str() << std::endl;
std::wcout << L"File name : " << FileName.c_str() << std::endl;
std::wcout << L"Extension : " << Extension.c_str() << std::endl;
std::wcout << L"Error value returned : " << Error << std::endl;
std::wcout << L"Did error occur? : " << (Error == EINVAL) << std::endl;
std::wcout << L"Value of 'EINVAL' : " << EINVAL << std::endl;
Run Code Online (Sandbox Code Playgroud)
上面代码的实际输出:
Full path to be splitted : \\?\K:\A Directory\Another Directory\File Name.After Period.extension
Drive :
Directory : \\?\K:\A Directory\Another Directory\
File name : File Name.After Period
Extension : .extension
Error value returned : 0
Did error occur? : 0
Value of 'EINVAL' : 22
Run Code Online (Sandbox Code Playgroud)
它如何处理短路径:
Full path to be splitted : K:\A Directory\Another Directory\File Name.After Period.extension
Drive : K:
Directory : \A Directory\Another Directory\
File name : File Name.After Period
Extension : .extension
Run Code Online (Sandbox Code Playgroud)
长路径的预期行为:
Full path to be splitted : \\?\K:\A Directory\Another Directory\File Name.After Period.extension
Drive : K:
Directory : \A Directory\Another Directory\
File name : File Name.After Period
Extension : .extension
Run Code Online (Sandbox Code Playgroud)
是否有_wsplitpath_s()支持长路径命名约定的替代方案?
按给定顺序接受 STL 算法、Win32 API 函数或 C 库函数。
| 归档时间: |
|
| 查看次数: |
2359 次 |
| 最近记录: |