问题描述
当你打开一个txt文件与FOPEN
有什么方法来删除一个文件中一些字符串而无需重写。
When you open a .txt file with fopenIs there any way to delete some strings in a file without rewriting.
例如,这是txt文件,我将与fopen()函数打开;
For example this is the txt file that i will open with fopen() ;
-------------
1 some string
2 SOME string
3 some STRING
-------------
我想删除它的第一个字符是2行并将其更改为
i want to delete the line which's first character is 2 and change it into
-------------
1 some string
3 some STRING
-------------
我的解决方案;
首先读取所有数据并保存在字符串变量。然后用FOPEN黑白模式相同的文件。并且除2号线试写入数据(不过这是不符合逻辑的我正在寻找C的一个更简单的方法...)
(我希望我的英语不是问题)
My solution is;First read all data and keep them in string variables. Then fopen the same file with w mode. And write the data again except line 2. (But this is not logical i am searching for an easier way in C ...)(i hope my english wasn't problem)
推荐答案
用最简单的方法可能是内存映射整个文件的 MMAP 。随着MMAP您可以访问该文件作为一项长期的内存缓冲区,可以在磁盘上被反射的变化修改。然后你就可以找到该行的偏移和移动很多字节回覆盖该行的文件的整条尾巴。
The easiest way might be to memory-map the whole file using mmap. With mmap you get access to the file as a long memory buffer that you can modify with changes being reflected on disk. Then you can find the offset of that line and move the whole tail of the file that many bytes back to overwrite the line.
这篇关于有没有删除从打开的文件的任何东西根本途径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!