碳二元文件:截断或以修改其结束调整 [英] C binary file : truncate or resize in order to modify its end

查看:184
本文介绍了碳二元文件:截断或以修改其结束调整的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 FILE *文件保存一些二进制数据。比方说,这个数据是双列表和最后一个项目是描述那些是什么双重的字符串。我想修改这个字符串(新的字符串可能更短)。首先,我删除旧的字符串。我需要查找的字符串的起点:

I have a FILE* file that holds some binary data. Let's say that this data are a list of double and that the last entry is a string that describes what are those double. I want to modify this string (the new string might be shorter). So first i delete the old string. I need to find the starting point of the string :

fseek(file,-size(sring.size()),SEEK_END);

和那我应该怎么办?我发现删除文件环节结束,但我不知道该用哪一个?一旦文件被重新大小,可以用我简单地写我的新字符串 FWRITE

and then what should i do ? i found Delete End of File link but i don't know which one to use... Once the file is re-sized, can i simply write my new string using fwrite ?

推荐答案

如果您要调整的文件,那么 ftruncate() http://www.linuxmanpages.com/man2/ftruncate.2.php )是你的功能寻找。你需要调用的fileno() FILE * 结构来获取<$ C $文件描述符C> ftruncate(),虽然

If you want to resize a file, then ftruncate() (http://www.linuxmanpages.com/man2/ftruncate.2.php) is the function you're looking for. You'll need to call fileno() on the FILE * structure to get the file descriptor for ftruncate(), though.

至于追加新的数据(新的字符串)一旦文件大小减少,只是寻求结束( fseek的(文件,0,SEEK_END) )和的fwrite()ING也应该这样做。

As for appending the new data (the new string) once the file has been reduced in size, just seeking to the end (fseek(file, 0, SEEK_END)) and fwrite()'ing there should do it.

编辑:记得打电话 fflush()截断文件之前

remember to call fflush() before truncating the file!

这篇关于碳二元文件:截断或以修改其结束调整的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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