我试图从文件中获取字符串的其余部分,以便将字符串存储在变量中。例如,第一行是"1234上海,中国“,但变量查询只得到”上海“,而不是”上海,中国“。体重增加1234。我猜这和城市和乡村之间的空间有关。
while (!file.eof())
{
string query;
long weight;
file >> weight >> query;
Term inputTerm(query,weight);
}发布于 2016-04-16 21:11:20
就像这样:
long weight;
char query[100];
while (file >> weight)
{
file.getline(query, 100);
Term inputTerm(std::string(query), weight);
}发布于 2016-04-16 21:11:49
您可以用这个来读取行的其余部分:
std::string ReadLine(std::ifstream& file){
char buf[1024]; //Unfortunately this means you can only have a max of 1024 char string
file.getline(&(buf[0]),1024,'\n');
return std::string(buf);
}像这样使用:
file >> weight;
query = ReadLine(file);https://stackoverflow.com/questions/36669645
复制相似问题