如何保持文件打开并从我们上次离开的地方继续?C++

How to leave a file open and continue from where we left last time? C++

本文关键字:方继续 继续 C++ 离开 文件 何保持 我们      更新时间:2023-10-16

有没有办法从文件读入直到一行末尾,然后转到另一个函数做某事,然后返回从同一个文件读入,但从我们上次停止的地方(不是从文件的开头)?

如果是,请提供片段。对我来说,代码比文字更有意义。谢谢

使用 std::fstream::open() 打开文件进行写入时,您可以选择将文件openmode设置为文件名后面的第二个参数...

std::fstream file;
file.open("myfile.txt", ios_base::openmode::ate);

openmode::ate 是用于打开输入光标位于 eof 的文件的标志。

http://www.cplusplus.com/reference/ios/ios_base/openmode/http://www.cplusplus.com/reference/fstream/fstream/open/http://www.cplusplus.com/reference/fstream/fstream/