Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
612 views
in Technique[技术] by (71.8m points)

Continue writing to a file with max size in C++

I want to write to a text file with limited size (1KB for example) and when the file reaches the maximum size I want the first half to be removed and continue writing to the file (append to what remained after removing first half). is this possible in C++?

for example: file that reached maximum size: 1 2 3 4 5 6 and I want to continue writing [7,9] the new file will look like: 4 5 6 7 8 9


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You could use a dedicated logging library for example spdlog.

https://github.com/gabime/spdlog

It has a ton of features, including rotating logs. You can define how big a log file should be, and how many logfiles you want. It can automatically discard the older logs.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...