CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    May 2009
    Posts
    4

    C# Help! Anyone know how to clear a text file?

    Hi all,

    I just need to know how to clear everything in a text file using c++.

    I tried deleting it, with File.Delete(Patch + navn + ".txt");
    but all the content got saved into another text file :S... So i think I should clear the .txt file before deleting it. Anyone know how to do in c#?


    Thanks.

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: C# Help! Anyone know how to clear a text file?

    Quote Originally Posted by Zeak View Post
    Hi all,

    I just need to know how to clear everything in a text file using c++.

    I tried deleting it, with File.Delete(Patch + navn + ".txt");
    but all the content got saved into another text file :S... So i think I should clear the .txt file before deleting it. Anyone know how to do in c#?


    Thanks.
    You say you need an answer in C++, but it looks like your code is in C#. Which one is it? If it is C#, File.Delete works as you would expect, it does not copy the file before deleting it.

  3. #3
    Join Date
    May 2009
    Posts
    4

    Re: C# Help! Anyone know how to clear a text file?

    Im using C# sorry,

    I did file.delete then after the textfile got deleted, I have a code to create a new textfile, but the content of the deleted text file is save/appended to the new textfile.

    So i was wondering if anyone know how to clear text file with C#. My solution is to clear text it before deleting it.
    Last edited by Zeak; May 21st, 2009 at 01:22 PM.

  4. #4
    Join Date
    May 2009
    Posts
    1

    Re: C# Help! Anyone know how to clear a text file?

    Hi Zeak,

    The way I code it is to replace the existing texts with blanks or delete all the texts.

    Vo Duc Dien

  5. #5
    Join Date
    May 2009
    Posts
    4

    Re: C# Help! Anyone know how to clear a text file?

    Quote Originally Posted by vodien View Post
    Hi Zeak,

    The way I code it is to replace the existing texts with blanks or delete all the texts.

    Vo Duc Dien
    But how would you code this in c#? How to replace the existing text with blanks in a textfile?
    Last edited by Zeak; May 21st, 2009 at 02:23 PM.

  6. #6
    Join Date
    Jun 2008
    Posts
    2,477

    Re: C# Help! Anyone know how to clear a text file?

    This will do what you want. You need to open the stream using FileMode.Create, which will create a new file, or write over an existing one. So, assuming that "C:\test.txt" already exists and is filled with some text, this will have the same effect as deleting all of that text.

    Code:
    using ( FileStream stream = new FileStream( @"C:\test.txt", FileMode.Create ) )
    using ( TextWriter writer = new StreamWriter( stream ) )
    {
        writer.WriteLine( "" );
    }

  7. #7
    Join Date
    May 2009
    Posts
    4

    Re: C# Help! Anyone know how to clear a text file?

    Thx BigEd, and everyone else who helped!

    Problem solved!

  8. #8
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: C# Help! Anyone know how to clear a text file?

    Quote Originally Posted by BigEd781 View Post
    Code:
    using ( FileStream stream = new FileStream( @"C:\test.txt", FileMode.Create ) )
    using ( TextWriter writer = new StreamWriter( stream ) )
    {
        writer.WriteLine( "" );
    }
    cool, thx, I didn't know that it's possible to write multiple usings
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured