所以我试图利用istringstream来解析文本文件.我们的想法是按空间分解每一行,并根据子串做事.代码工作正常,除了两件事,它重复计算每一行的最后一个子字符串,当它完成读取文件时它会出错.我之前没有使用过sstream,所以任何见解都会有所帮助.
file.getline(str,80);
while(!file.eof())
{
cout<<str<<endl;
istringstream iss(str);
while (iss)
{
iss >> sstr;
cout << "Substring: " <<sstr << endl;
}
file.getline(str,80);
}
Run Code Online (Sandbox Code Playgroud)
该while循环应该是这样的:
std::string line;
while (std::getline(file, line))
{
std::istringstream iss(line);
std::string token;
while (iss >> token)
{
cout << "Substring: " << token << endl;
}
}
Run Code Online (Sandbox Code Playgroud)
该getline输入操作返回的流对象,其本身所具有的专门化的转换为BOOL指示操作是否成功,而当你已经达到了各自流的末尾,它将准确地失败.
| 归档时间: |
|
| 查看次数: |
1672 次 |
| 最近记录: |