首页 > 解决方案 > 如何通过用空格('')分隔文件路径来循环字符串并将其存储到C++中的向量/数组中

问题描述

我有一个具有多个文件路径的字符串,如下所示:

%programdata%\EMR\Registration\Registration_EMR.xml C:\ProgramData\EMR\Registration\RegistrationEMR.xml
%AppData%\EMR\EMR Setup\REGDATA\registration_EMR.xml %AppData%\EMR\EMR Setup\REGDATA\RegistrationEMR.xm

我想通过用空格('')分隔文件路径来循环字符串并将其存储到向量/数组中。

以便向量/数组包含以下用空格分隔的路径。

%programdata%\EMR\Registration\Registration_EMR.xml 
C:\ProgramData\EMR\Registration\RegistrationEMR.xml
%AppData%\EMR\EMR Setup\REGDATA\registration_EMR.xml 
%AppData%\EMR\EMR Setup\REGDATA\RegistrationEMR.xml

有人可以帮我吗?

更新代码:

我修改了 ReadJsonFile 函数,如下所示,在每个 .xml 之后拆分文件路径,然后调用 ExpandEnvironmentStrings,然后将每个文件路径存储到向量中。

我在标记字符串时遇到了困难,因为我们只能标记 char*。

我收到以下错误:

“const WCHAR *”类型的参数与“char *”类型的参数不兼容,重载函数“ExpandEnvironmentStringsW”的实例不匹配参数列表

bool EMRFileReader::ReadJsonFile(const std::wstring &strFilePath, std::wstring &strFileContent)
{ 
std::vector<std::wstring> pathsVector;  
const WCHAR *wpszPathToSearch = strFilePath.c_str();    
TCHAR szOut[MAX_PATH];  
char *token = strtok(wpszPathToSearch, ".xml");  
//argument of type "const WCHAR *" is incompatible with parameter of type "char *"  
while (token != NULL)   
{       
ExpandEnvironmentStrings(token, szOut, ARRAYSIZE(szOut)); 
//no instance of overloaded function "ExpandEnvironmentStringsW" matches the argument list

pathsVector.push_back(szOut);   
}
return true;
}

标签: c++c++11c++14

解决方案


推荐阅读