为什么不能从C ++中的文件中删除内容? [英] Why is it not possible to erase contents from a file in C++?

查看:213
本文介绍了为什么不能从C ++中的文件中删除内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我会理解这种语言是否会限制我将新内容添加到文件中不管位置),因为它会导致碎片化。
但是我不明白是为什么不可能:


  1. 擦除最后一行的内容,从EOF。 [ASCII / BINARY]

  2. 从文件[ASCII / BINARY]中间部分清除内容

  3. 使用其他文字替换文件中的文字size [ASCII]

  4. 将文件中的数据替换为大小相同的其他数据[二进制]

有任何其他语言支持此功能吗?



编辑:要在C ++中执行此操作,需要读取文件,对变量,然后创建一个新文件。问题是为什么不能编辑原始文件而不是创建另一个文件。

解决方案


有任何其他语言支持此功能吗?


C ++支持。你可以寻求,你可以写。但事实是,这是一个非常罕见的情况,替换相同的确切的字节长度和事实,用一组字节替换一组不同长度的字节是一个点是非常低效的写一个新文件是更快,导致存在的语言和功能。



其他语言可能会有不同的处理方式,但我还没有学到一个。因为文件处理不会跨语言而改变,并且所有语言和框架写作者得出相同的结论:文件不是插入和删除数据的最佳媒体。



如果您需要插入和删除,请使用另一个容器(列出来了)。如果您需要将此容器保留到磁盘,请在所有操作完成后执行此操作。


The title pretty much says it all, but still to elaborate:

I would have understood if the language would have restricted me from adding new contents to a file (irrespective of position) because it would lead to fragmentation. But what I do not understand is why it is not possible to:

  1. Erase contents from the last line, similar to backspacing from EOF. [ASCII/BINARY]
  2. Erase contents from middle portion of file [ASCII/BINARY]
  3. Replace text in a file with some other text of same size [ASCII]
  4. Replace data in a file with some other data of same size [Binary]

Does any other language support this?

EDIT: To do this in C++, you need to read the file, perform the modifications on variables, then create a new file. The question was why it is not possible to edit the "original" file instead of creating another file.

解决方案

Does any other language support this?

C++ supports it. You can seek and you can write. But the fact that this is a very uncommon scenario to replace the same exact byte length and the fact that replacing a bunch of bytes with another bunch of bytes that has a different length is horribly inefficient to a point were writing a new file is faster, lead to the language and functions that exist.

Other languages may handle this differently, but I have yet to learn one that actually does. Because file handling does not change across languages and all language and framework writers came to the same conclusions: files are not the best medium for insertion and deletion of data. Arrays are not the best medium for insertion and deletion, files just being one them.

If you need to insert and delete, use another container (lists come to mind). If you need to persist this container to disk, do so after all manipulation is done.

这篇关于为什么不能从C ++中的文件中删除内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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