|
-
May 21st, 2009, 01:07 PM
#1
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.
-
May 21st, 2009, 01:11 PM
#2
Re: C# Help! Anyone know how to clear a text file?
 Originally Posted by Zeak
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.
-
May 21st, 2009, 01:15 PM
#3
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.
-
May 21st, 2009, 02:10 PM
#4
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
-
May 21st, 2009, 02:20 PM
#5
Re: C# Help! Anyone know how to clear a text file?
 Originally Posted by vodien
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.
-
May 21st, 2009, 03:59 PM
#6
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( "" );
}
-
May 21st, 2009, 04:31 PM
#7
Re: C# Help! Anyone know how to clear a text file?
Thx BigEd, and everyone else who helped!
Problem solved!
-
May 22nd, 2009, 07:57 AM
#8
Re: C# Help! Anyone know how to clear a text file?
 Originally Posted by BigEd781
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|