Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
582 views
in Technique[技术] by (71.8m points)

c++ - 如何通过使用space('')分隔文件路径来遍历字符串并将其存储到C ++中的向量/数组中(How to loop through the string by separating the file paths with space(' ') and store it into the vector/array in C++)

I have a string that has multiple file paths like as shown below:

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

%programdata%EMRRegistrationRegistration_EMR.xml C:ProgramDataEMRRegistrationRegistrationEMR.xml
%AppData%EMREMR SetupREGDATA
egistration_EMR.xml %AppData%EMREMR SetupREGDATARegistrationEMR.xm

I wanted to loop through the string by separating the file paths with space(' ') and store it into the vector/array.

(我想通过使用space('')分隔文件路径来遍历字符串,并将其存储到向量/数组中。)

so that the vector/array contains the below path that is separated with space.

(因此向量/数组包含以下路径,并用空格分隔。)

%programdata%EMRRegistrationRegistration_EMR.xml 
C:ProgramDataEMRRegistrationRegistrationEMR.xml
%AppData%EMREMR SetupREGDATA
egistration_EMR.xml 
%AppData%EMREMR SetupREGDATARegistrationEMR.xml

Could anyone please help me?

(谁能帮我吗?)

Updated code:

(更新的代码:)

I modified the ReadJsonFile function like as shown below to split the file paths after each .xml, then calling ExpandEnvironmentStrings and then storing each file path into the vector.

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

I am facing difficulty to tokenize the string as we can tokenize only char*.

(我现在难以对字符串进行标记化,因为我们只能对char *进行标记化。)

I am getting the below errors:

(我收到以下错误:)

argument of type "const WCHAR *" is incompatible with parameter of type "char *" no instance of overloaded function "ExpandEnvironmentStringsW" matches the argument list

(类型“ 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;
}
  ask by John Paul Coder translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
#include<string>
#include<vector>


int main(){
    std::string paths
    {R"rawstring(%programdata%EMRRegistrationRegistration_EMR.xml C:ProgramDataEMRRegistrationRegistrationEMR.xml%AppData%EMREMR SetupREGDATA(
egistration_EMR.xml %AppData%EMREMR SetupREGDATARegistrationEMR.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;

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...