C ++使用不同的流读写同一文件 [英] C++ read and write to same file using different streams

查看:61
本文介绍了C ++使用不同的流读写同一文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个流指向同一文件.第一个是 std :: ofstream操作系统,第二个是 std :: ifstream是,均以二进制模式打开.

I have two streams pointing to the same file. The first one is std::ofstream os and the second is std::ifstream is, both opened in binary mode.

我正在使用 os 创建一个新文件.文件创建过程要求我(有时)读取由 os 写入文件的数据. is 流查找到所需的位置,读取一些数据,然后 os 以其(不同的)偏移量执行操作,然后刷新.

I'm using os to create a new file. The file creation process requires me to (at times) read data that was written to the file by os. The is stream seeks to the needed position, reads some data, and then os does it's thing at its (distinct) offset and then flushes.

这样做合法吗?这些流会彼此脚吗?

Is this legal to do? Will the streams stomp on each other?

推荐答案

无法引用任何标准,但是/因为这是特定于平台的
(也许是互斥访问,正在不同级别上缓冲...)

Can´t quote any standard, but/because this is platform specific
(maybe exclusive access, buffering on different levels...)

您可以在 ios :: in | ios :: out
中使用单个 fstream 并在采取任何行动之前寻求帮助.

You could just use a single fstream with ios::in|ios::out
and seek before every action.

这篇关于C ++使用不同的流读写同一文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆