Hi all,

In my application i will be writing data to a text file periodically (assume every 5 min.)
after some time i want clear the old contents without halting the application and with out deleting the file.

thanks in advance
johny

Dani AI

Generated

You do not need to delete or recreate the file to clear it. If you already have the file open, you can truncate it in place. On POSIX systems, call ftruncate on the file descriptor; this leaves the same file inode and path in place and does not require the process to halt.

#include <fcntl.h>
#include <unistd.h>

int fd = open("johny123.txt", O_WRONLY | O_CLOEXEC);
if (fd >= 0) {
    // Clear the file to zero length without deleting it
    if (ftruncate(fd, 0) == 0) {
        lseek(fd, 0, SEEK_SET); // reposition to start for subsequent writes
    }
    // keep fd and continue writing as usual...
}

For Windows, the equivalent is _chsize_s on a file handle obtained via _fileno or _open (see _chsize_s documentation).

Important constraint: files are linear byte arrays. You can only truncate from the end. There is no in-place way to remove the first day’s bytes and keep later bytes without rewriting. To drop older data while keeping recent data, copy the portion you want to keep to a temporary file, fsync it, then atomically replace the original with rename and reopen your stream; rename on the same filesystem is atomic on POSIX (POSIX ftruncate, rename(2)). When cutting by day, find a safe boundary (e.g., end of a line) before copying to avoid splitting log entries.

Recommended Answers

All 6 Replies

> and with out deleting the file.
What's this supposed to mean?

Or more to the point, what's a "clean" text file with anything other than zero bytes in it supposed to look like?

When ready to clear the file out just close it and reopen with the truncate flag.

Hi,
I will be writing data to text file continuously ( assume 4-5days)
eg: file name johny123.txt
and data will be keep on appending to the text file.
on 2nd day I want to delete ( clear ) the contents of 1st day.
using C code I am clear when to clear the file, but i am not clear how to clear the contents of text file partially.
so that memory of text file will be reduced.

Thanks in advance

> and with out deleting the file.
What's this supposed to mean?

Or more to the point, what's a "clean" text file with anything other than zero bytes in it supposed to look like?

re-read my post -- all you have to do is close the file then reopen it with "w" flag. Pretty simple stuff.

hiya it just opens the file and if it fails to do so reports an error message.

thanks for the last bit of advice Aia

timestamp your logs by date (or week, month, etc.) then you can delete the old ones at your leisure.

you can keep the most current log name without a timestamp if you like, then before creating a new one the next day (or week, month, etc.) just rename it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.