我是 C++ 的新手,我有一个问题。我正在研究 fstream。我有一个 data.txt 文件有这样的内容
[exe1]
1 0 2 9 3 8 4 7 5 6
[exe2]
1 0 2 9 3 8 4 7 5 6:0
[exe3]
23
[exe4]
Micheal
我的问题是如何读取特定行,例如第 2 行:
1 0 2 9 3 8 4 7 5 6.
提前致谢
最佳答案
如果找到你的行的唯一方法是在其中搜索一些数据,那么你将不得不阅读每一行,直到找到感兴趣的行:
std::ifstream fs("data.txt");
std::string line;
while(std::getline(fs, line)) {
if ( line.find("[exe2]") != std::string::npos )
{
if ( std::getline(fs, line) ) {
// line found
// line should contain "1 0 2 9 3 8 4 7 5 6:0"
break;
}
}
}
关于c++ - 如何使用 fstream 读取文件的特定行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36600864/