打开文件进行读写的好处
任何人都可以向我指出一些关于打开文件进行读写的利弊的讨论,而不是(例如)打开文件进行读取,关闭它,然后重新打开进行写入。我尝试过寻找深入的信息,但并不快乐。
非常感谢
Can anyone point me to some discussion covering the pro's and con's of opening a file for read and write as opposed to (for example) opening a file for read, closing it and then reopening for write. I've tried searching for in-depth information without joy.
Many thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这取决于你在做什么。打开读取和写入可能更难获得正确和一致,因为很容易意外截断文件或无意中覆盖部分数据。
如果读取然后写入(大概是完全替换)实际上是一种选择,那么打开两个单独的文件可能会更简单,但请考虑写入新文件并在成功时重命名,以确保如果出现某些情况(甚至是编程错误!)中断写入旧数据不会丢失。
如果您决定打开以进行读/写,请务必仔细阅读文档,以免在打开时截断文件,并注意查找和告知功能。除非您以二进制模式打开,否则您只能安全地通过寻找先前告诉返回的位置。
It depends on what you're doing. Opening for read and write can be harder to get correct and consistent as it's very easy to accidentally truncate the file or overwrite parts of the data unintentionally.
If reading and then writing (presumably a complete replacement) is actually an option, then two separate file opens may well be simpler, but consider writing to a new file and renaming if successful to make sure that if something (even a programming error!) interrupts the write the old data isn't lost.
If you do decide to open for read/write make sure to read the documentation carefully so that you don't truncate the file on opening and take care with seek and tell functions. Unless you open in binary mode you can only safely pass to seek a position that was returned by a previous tell.
我建议最合理的原因是竞争条件。
假设两个用户要访问同一个文件并对其进行编辑。用户 A 打开该文件,读入并关闭它。在用户 A 有机会写入之前,用户 B 就出现了,并且他还读取了文件。
现在,如果用户 A 将文件保持打开状态,用户 B 就无法访问该文件进行读取。在许多情况下,这是期望的行为,因为它避免了任何可能的竞争条件。在用户 A 使用完该文件之前,用户 B 无法访问该文件。
或者,我们假设用户 A 和 B 不会重写文件中的任何内容,而只会追加到文件中,并且他们这样做的顺序并不重要(日志记录就是一个很好的例子)。在这种情况下,我们在用户 A 写入之前授予用户 B 访问该文件的权限是有意义的,因为用户 B 可能不关心用户 A 当前正在做什么。
显然,这两种情况都与您想要如何处理竞争条件有关,因此您应该首先考虑这个问题。通常阻止行为可以防止竞争条件损害您的程序,因此我会默认该行为,但请注意其他选择。
I would suggest the most plausible reason being race conditions.
Assume two users are going to access the same file and edit it. User A opens the file, reads it in and closes it. User B comes along before User A has had a chance to write and he also reads in the file.
Now if User A had left the file open User B could not access it to read. In many cases this is the desired behavior as it avoids any possible race conditions. User B cannot access the file till User A is done with it.
Alternatively, let us assume that Users A and B will not rewrite anything in the file but only append to it, and that the order in which they do so is unimportant (logging is a good example of this). In this case it makes sense for us to give User B access to the file before User A has written as User B maybe unconcerned with what User A is currently doing.
Obviously both cases are concerned with how you want to handle the race condition, so you should think about that problem first. Usually blocking behavior prevents race conditions from hurting your program, so I would default to that behavior, but be aware of the alternatives.
速度。
但这取决于你在做什么。
如果您要写很多小东西,最好保持打开状态。
如果您要写入大量数据,那么最好在每次需要时加载它。
读书也是如此。
Speed.
But it depends on what you're doing.
If you're going to write a lot of small things, it's better to keep it open.
If you're going to write a big chunk of data, it's (perhaps) better to load it every time you need it.
The same goes for reading.
由于没有人提到它,我将采用显而易见的方法 - 因为您想同时读取和写入文件。
考虑像 ISAM 文件这样的东西,您想要在其中读取、添加、删除、替换各种偏移处的记录、更新索引等。通过寻找正确的位置而不打开和关闭文件,可以更轻松地完成任务!
Since no one has mentioned it, I'll go with the obvious - because you want to read and write to the file at the same time.
Consider something like an ISAM file where you want to read, add, delete, replace records at various offsets, update the indexes, etc. A whole lot easier to do by seeking to proper places without opening and closing the files!
如果您打开文件进行读写,需要注意的另一件事是您需要在读写操作之间切换时刷新流
Another thing that needs to be taken care of if you open the file for reading and writing is that you need to flush your stream while switching between read and write operations