我正在尝试用 C++ 创建简单的应用程序。此应用程序必须从文件中读取并显示数据。我写了函数:
std::vector <AndroidApplication> AndroidApplication::getAllApp(){
std::vector<AndroidApplication> allApp;
std::fstream f;
f.open("freeApps.txt");
std::string line;
if(f.is_open()){
while(getline(f, line)) {
std::string myLine = "";
char * line2 = line.c_str();
myLine = strtok(line2,"\t");
AndroidApplication * tmpApp = new AndroidApplication(myLine[1], myLine[2], myLine[4]);
tmpApp->Developer = myLine[0];
tmpApp->Pop = myLine[3];
tmpApp->Type = myLine[5];
allApp->pushBack(tmpApp);
}
}
return allApp;
}
它抛出一个错误:
myLine = strtok(line2,"\t");
一个错误:
cannot convert from 'const char *' to 'char *'
你能告诉我我该如何处理吗?
最佳答案
不要使用strtok
。 std::string
有自己的字符串扫描函数,例如,find
。
关于c++ - 如何将类型从 const char * 转换为 char *,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8713992/