We help IT Professionals succeed at work.

erase contents of file

bachra04 asked
Last Modified: 2012-02-09
Hi experts,

I need to know how to erase the contents of a file in c and then write new information.

(see attached code )

Watch Question

Hey bachra04,

Using StreamWriter would create a new file and if the file already exists, The content of the file will be erased. You can also use StreamWriter to append text to a file but you want to remove the content. The following code is used in the C# Programming Language

First, We need to define our object.

System.IO.StreamWriter SWriter = new System.IO.StreamWriter("Path to the file needs to be erased")

Open in new window

Then, We should close the StreamWriter in order to access this file by another process or application


Open in new window

Thank you, I hope this helps :)
Most Valuable Expert 2014
Top Expert 2015

I don't see attached code, but
#include <stdio.h>
 FILE *F=fopen("file","w");
 fputs("new information",F);

Open in new window

will erase the contents of a file in c and then write new information



Open in new window

 if ((fp = fdopen(pfd, "r+")) == NULL) {
              err(EXIT_FAILURE, "fdopen");

          pollv[0].fd = pfd;
          pollv[0].events = POLLIN;
          pollv[0].revents = 0;

       while(1) {

             rc = poll(pollv, 1, 15000);

                if (rc > 0 && pollv[0].revents)
                    if ((rc = read(pfd, result, sizeof(result))) == -1) {
                          printf(" INE 98 error read");

                     if (strcmp(res, "val1"))

                        // here is my problem I want to erase the contents of the file
                        // then write the following
                                      "mama", "232", 22);




That was my attached code the difficulty I had is that I cannot close and open the file at the same time.

Before the fprintf

rewind (fp);
Senior Software Engineer
This one is on us!
(Get your first solution completely free - no credit card required)


First I open the file to read from it
then based on the data I got I will erase the data
and then I need to write new data to it.

what you are suggesting is that I need to close the file
then open it in write Mode then put my data ?
Michael EagerConsultant

> what you are suggesting is that I need to close the file then open it in write Mode then put my data ?

If you open the file with "r+" you can both read and write to the file.

As @cup said, after you read the data from the file, rewind it to the beginning and then write your new data.  When you close the file, it will be truncated to the correct size.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.