[英]How to read specific line of file using fstream?
我是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;
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.