【发布时间】:2020-07-04 14:10:19
【问题描述】:
我正在学习用C++读写文件,发现一个问题。
我的 test.txt 文件在 3 行中包含 3 个字符串:
abc
def
mnp
我的问题是:我不明白为什么我需要使用f.seekg(2, ios::cur);
而不是f.seekg(1, ios::cur);
我知道如何在 c++ 中使用seekg(),我认为我只需要忽略 1 个字节
通过getline() 函数获取下一行。
这是我的代码:
ifstream f;
f.open("D:\\test.txt", ios::in);
string str1, str2, str3;
f >> str1;
f.seekg(2, ios::cur);
getline(f, str2);
getline(f, str3);
cout << str1 << " " << str2 << " " << str3 << endl;
【问题讨论】:
-
你根本不需要寻找
-
那为什么代码是
2呢?你是从哪里弄来的?当你问他们时,作者对此有何看法?