如何通过使用space('')分隔文件路径来遍历字符串并将其存储到C ++中的向量/数组中

问题描述 投票:0回答:1

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

%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++11 c++14
1个回答
0
投票
#include<string>
#include<vector>


int main(){
    std::string paths
    {R"rawstring(%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)rawstring"};

    std::vector<std::string> pathsVector;

    for(size_t index=0,from{};index<4;++index){


        pathsVector.push_back(paths.substr(from,paths.find(".xml",from)+4));
        from=paths.find(".xml",from)+4;


    }

    return 0;

}
© www.soinside.com 2019 - 2024. All rights reserved.