绝对安全的覆盖保存文件的方法? [英] Adequetely safe method of overwriting a save file?

查看:103
本文介绍了绝对安全的覆盖保存文件的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用cstdio,覆盖文件的最安全方法是什么?在这种情况下,安全"是指文件不可能不完整或损坏;该文件将被完全覆盖,或者如果出现问题,将成为旧文件.

Using cstdio, what is the safest way of overwriting a file? 'safe' in this case meaning that there's no chance the file will become incomplete or corrupted; the file will either be the completely overwritten, or it will be the old file should something have gone awry.

我想最好的方法是创建一个临时中间文件,然后在中间文件完成后覆盖旧文件.如果这实际上是最好的方法,那么尽管有一些罕见的问题,但似乎还有其他一些问题.

I imagine the best way to do this, would be to create a temporary intermediate file, then overwrite the old file once that intermediate is complete. If that actually is the best way though, there's a few other problems that'd seem possible, if albeit rare.

  • 如果覆盖时程序退出,我怎么知道要使用另一个文件?
  • 如果程序在创建过程中退出,我怎么会不使用其他文件?
  • 我怎么知道原始文件或 中间体处于未定义状态(因为它可能会以某种方式失败 仍然可读,但其中包含的数据确实有误)?
  • How would I know to use this other file should the program quit while overwriting?
  • How would I know to NOT use the other file should the program quit during it's creation?
  • How would I know the original file or the intermediate is in an undefined state (since it may fail in a way that remains readable but the data it contains is subtly wrong)?

我想有一个好的做法,但是我找不到.这用于保存游戏数据;只有一个文件,并且每次都将覆盖整个文件,没有部分覆盖或后顾之忧.

I imagine there's a single good practice for this, but I haven't been able to find it. This is for saved game data; there's only one file, and the entire file is overwritten every time as well, there are no partial overwrites or appending to worry about.

推荐答案

就像其他人所说的那样,保留现有文件,然后写入一个新文件.如果这非常重要(也就是说,用户可能无法恢复信息),请确保周围也有一个备份"文件(例如,如果您的程序保存了abc.config,请保留abc.old.config [[如果您想保证名称在任何地方都可以使用,则.cfg.bak可能是更好的选择]].

As others have said, keep the existing file around, and write to a fresh file. If it's very important (that is, the user can't possibly recover the information), make sure that there is a "backup" file around as well (e.g. if your program saves abc.config, leave an abc.old.config or abc.backup [if you want guarantees that the name works everywhere, .cfg and .bak may be better choices]).

在写入文件时,在文件中放入某种结束标记,以确保文件完整.如果要避免文件的用户编辑",则可能还需要具有内容(sha1,md5或类似内容)的校验和.如果结尾标记不存在,或者校验和错误,那么您知道该文件为错误"文件,因此请不要使用它.进行备份.

When you write the file, put some sort of endmarker in the file, so that you can be sure that the file is complete. If you want to avoid "user editing" of the file, you may also want to have a checksum of the content (sha1, md5 or similar). If the endmarker isn't there, or the checksum is wrong, then you know that the file is "bad", so don't use it. Go for the backup.

  1. 将新内容写入临时文件(例如fstream fout("abc.tmp");)
  2. 删除备份文件(如果存在)(例如remove("abc.bak");)
  3. 将现在较旧的文件重命名为备份名称(例如rename("abc.cfg", "abc.bak");)
  4. 将新文件重命名为旧文件(例如rename("abc.tmp", "abc.cfg");
  1. Write the new conten to a temporary file (eg. fstream fout("abc.tmp");)
  2. Delete the backup file (if it exists) (e.g remove("abc.bak");)
  3. Rename the now old file to the backup name (e.g. rename("abc.cfg", "abc.bak");)
  4. Rename the new file to the old one (e.g. rename("abc.tmp", "abc.cfg");

对于所有步骤(尤其是写入实际数据),请检查是否有错误.您需要确定哪里可以得到错误,哪里不可以(例如,不存在的文件的remove是好的,但是,如果rename不起作用,您可能应该停止,或者可能最终会导致不良后果).

For ALL steps (in particular writing the actual data), check for errors. You need to decide where it is OK to get errors and where it is not (remove of a file that doesn't exist is OK, for example, but if rename doesn't work you probably should stop, or you may end up with something bad).

加载文件时,请检查所有步骤,如果出错,请返回备份文件.

When loading the file, check all steps, if it goes wrong, go back to the backup file.

这篇关于绝对安全的覆盖保存文件的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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