使 std::fstream 写入文件末尾,但从头开始读取
Make std::fstream write to end of file but read from beginning
标题很好地解释了这一点。我有一个截断并写入的文件(作为控制文件初始内容的测试(。然后我想对该文件进行读/写操作。具体来说,我想写到文件的末尾,但从头开始阅读。
程序:
// (1) Make an initial file (truncated std::ofstream) with some contents
// (2) Close initial file stream
// (3) Re-open file with read and write permissions (std::fstream)
// (4) Set stream read pointer to beginning of file
// (5) Set stream write pointer to the end of file
这在某种程度上与问题一起暗示,但是我应该使用什么std::fstream::openmode
按位参数来打开文件(或者默认std::fstream::in | std::fstream::out
是否足够好(?
fstream 没有单独的读取和写入位置,在从读取更改为写入时需要查找。
seekg
和seekp
都调用pubseekpos
对于fstream
输入和输出执行相同的操作:https://en.cppreference.com/w/cpp/io/basic_filebuf/seekpos。
根据您的使用案例,同一文件上的单独读取和写入流可能有效。
相关文章:
- 如何从头开始创建 avi 文件?
- 使 std::fstream 写入文件末尾,但从头开始读取
- 如何继续读取不同功能中的文件,而不是从头开始再次读取文件?
- 如何使用 picojson.h 在 c++ 中从头开始创建 json 对象
- 尝试从头开始实现Leetcode的FizzBuzz多线程问题。收到"libc++abi.dylib: terminating"错误
- 从头开始为应用程序创建 docker 映像是否有意义?
- 停止和开始读取/复制二进制文件
- 如何创建一个循环,该循环检查每次迭代并在每次迭代后从头开始
- 如何在C++中从头开始反序列化文件(没有库)
- 从头开始为使用Qt的应用程序制作简约的构建文件
- SDL_Mixer再次播放时,从随机位置开始一段时间,然后从头开始
- 循环,使程序从头开始
- Bison-3.0.4 中的 Linux 从头开始错误 'make check'
- 从头开始为多目录项目制作 Cmake
- 链表:从头开始插入
- 从头开始创建C 背景Win32应用程序
- QPlainTextEdit-搜索文档到底,然后从头开始
- 从头开始绘制大圆圈
- 从第 3 位开始读取 int 的最快方法?
- 在C++中从头开始制作 TCP 套接字