我在unix.stackexchange上已经阅读了关于如何在文件中添加或删除行而不需要创建临时文件的各种问题/答案.@H_502_2@
https://unix.stackexchange.com/questions/11067/is-there-a-way-to-modify-a-file-in-place?lq=1@H_502_2@
似乎所有这些答案都需要一个至少读取到文件末尾,如果输入是一个大文件,这可能是耗时的.有没有解决的办法?我希望文件系统像链表一样实现……所以应该有办法达到所需的“行”,然后只添加东西(链接列表中的节点).我该怎么做呢?@H_502_2@
我这么认为是正确的吗?或者我错过了什么?@H_502_2@
Ps:我需要在’C’中完成,不能使用任何shell命令.@H_502_2@
最佳答案
您可以就地修改文件,例如使用dd.@H_502_2@
@H_502_2@
$echo Hello world,how are you today? > helloworld.txt
$cat helloworld.txt
Hello world,how are you today?
$echo -n Earth | dd of=helloworld.txt conv=notrunc bs=1 seek=6
$cat helloworld.txt
Hello Earth,how are you today?
问题是如果你的更改也改变了长度,它将无法正常工作:@H_502_2@
@H_502_2@
$echo -n Joe | dd of=helloworld.txt conv=notrunc bs=1 seek=6
Hello Joeth,how are you today?
$echo -n Santa Claus | dd of=helloworld.txt conv=notrunc bs=1 seek=6
Hello Santa Clausare you today?