开发者

Add lines at the end of file

开发者 https://www.devze.com 2022-12-10 07:37 出处:网络
I would like to add logs at the end of a file for each event, and create a new one w开发者_运维知识库hen its size up to 255 Mo.

I would like to add logs at the end of a file for each event, and create a new one w开发者_运维知识库hen its size up to 255 Mo.

For example, the current file could be /var/log/foo.2:

/var/log/foo.0.log (full log file)
/var/log/foo.1.log (full log file)
/var/log/foo.2.log

Have you got an idea of C source to do so?

Thank you


When opening the file with

File *fopen(const char *filename, const char *mode);

choose "a" as mode. Which will open or create a text file for writing at the end of file.


Basic c file code: http://c-programming.suite101.com/article.cfm/c_tutorial_file_handling_commands


If you just want to have the functionality, look at log4c.

If you want to know how that works, you can look at its code as well.

If you need specially tailored code for your application...

Basically you need to use fopen with the "a" option, where a stands for append.

To determine size of a file, use ftell or another platform specific function because there is no file size information method in the C standard as far as I can remember. Or you go the long way and just read all the bytes from the file and count them...

0

精彩评论

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