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
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Comments on How to clear the contents of a file?

Parent

How to clear the contents of a file?

+4
−0

How to clear the contents of a file using C?

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

Post
+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.

History
Why does this post require attention from curators or moderators?
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)
But what if I for instance want to delete a specific content from the text file using c program? Is t...
dumplings‭ wrote about 3 years ago

But what if I for instance want to delete a specific content from the text file using c program? Is there some function that I can use as I do not want to clear ALL the contents, thanks!

elgonzo‭ wrote about 3 years ago · edited about 3 years ago

dumplings‭, no, there is no such function available -- not as a single function. Modifying file content is highly specific to the individual application scenario or use case. Since this cannot be really well generalized to cover a wide gamut of use cases, you will be hard pressed to find any such a function in whatever general-purpose language/framework you will look at. You will have to implement the desired logic using a number of functions...

What is typically done in such a case is creating/opening a new file (with a new temporary file name), and then write/copy the desired content (in your case: the desired pieces from the original source file) into the new file. After the new file has been written and closed successfully (and only if all that happened successfully without errors), rename the now old original file to *.bak or something like that (or delete it, if you like to live precariously...), and rename the new file with the temporary name to the original file name.

dumplings‭ wrote about 3 years ago · edited about 3 years ago

The problem I currently have now is that I'm not sure how to get user input and then strcmp() with the contents in the text file to delete that particular string containing the substring like what the user inputs. I also read on strstr() and gets() and implemented them but only manage to extract the string from the text file containing that particular substring instead of deleting that string itself. How should I approach this?

elgonzo‭ wrote about 3 years ago · edited about 3 years ago

dumplings‭ Start with figuring out how to write strings/text lines to a (new) file. Don't think about finding and removing text lines, yet. Solve this task first. Exercise it, make it work successfully.

If you have accomplished that, figure out how to read a text line from a file and write it to another file, and doing that for all text lines line-by-line utilizing some kind of loop. Exercise it and make it work successfully as well. Still don't think about finding and removing text lines, yet.

A third task: Now, don't think about files. Don't even do or think about any file operations. Just define some textline strings in your C code and exercise strstr. Exercise situations where you define a substring that is in one/some of the defined textline strings, and exercise situations where you define a substring that is not in one of the defined textline strings. Only proceed with the next task if you familiarized yourself with strstr and exercised it successfully. (1/2)

elgonzo‭ wrote about 3 years ago · edited about 3 years ago

dumplings‭ (2/2) Only if you have accomplished these three tasks (write text lines to a file; read text lines from one file and write them to another file; using strstr) individually and independently, and only then, it is time for the concluding task: Combine the text line reading and writing with the strstr (basically, combining the second and third task i laid out).

Simply put, the concluding task is to read text lines from the original file on a line-by-line basis, and employ strstr to decide whether a text line read from the original file should be written to the new file, or whether it should not be written to the new file.


With regard to "I'm not sure how to get user input". I am a bit confused here. The code snippets in some of your other questions unambiguously show that you already know how to get user input, and i really have no idea what it is that makes you unsure about getting user input despite already knowing how to do it.