Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Notifications
Mark all as read
Q&A

How to clear the contents of a file?

+4
−0

How to clear the contents of a file using C?

Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

1 answer

+5
−0

Simply opening a file for writing (using fopen) will clear it (‘truncate to zero length’, per the standard). Only opening a file in read or append mode will preserve its contents.

See section 7.21.5.3 of a recent C standard (like this draft) to get it straight from the horse's mouth, or any of the various man pages or C tutorial sites you can find by searching for ‘fopen’ on the web.

Why does this post require moderator attention?
You might want to add some details to your flag.

1 comment thread

But what if I for instance want to delete a specific content from the text file using c program? Is t... (5 comments)

Sign up to answer this question »