开发者

Making file wrap around when using fwrite

I am using a开发者_运维知识库n embedded system running linux.I use a ramdisk filesystem on the embedded target. My application captures real-time data and does Standard C "fwrite" to a file in this ramdisk fs.As there is limited amount of memory , I would like to set a max size for the file and cause fwrite to wrap around like a circular buffer. Is there a way to do this in manner that is transparent to the application ? I would prefer the application to remain unchanged when I migrate to a filesystem on a storage device (eSATA) having much larger capacity.


There's no built in method of achieving this.

The best option is probably to write a small wrapper function that takes care of the file write while maintaining a count of the number of bytes written.

Once it has reached the maximum size that you set it should call rewind() (or fseek() etc.) to go back to the start of the file.


It might be easier to use mmap() to memory map your file and then treat it like a circular buffer. But again you would need to implement the wrapping yourself.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜