473,406 Members | 2,378 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

How to clear file content?!

How can I clear a text file without deleting and creating a new (empty) file?
Apr 30 '06 #1
3 15730
Okay, I'm going to have to guess what you mean by "clear a text file." From
the context of your message, it sounds like you mean to remove all the text
from it. Now, there are several ways to do this, but all of them involve
what you refer to as "deleting and creating a new (empty) file." That is, if
you have a text file, and you want to remove the text from it, and then save
it, this is what happens in either case:

1. Open the file, remove the text from it by setting it to "" and then save
it. When you re-save it, you are essentially overwriting the existing file
with a new one.
2. Create a new file with no text in it and save it over the original. When
you save it, you are essentially overwriting the existing file with a new
one.

The bottom line is, when you save a file, you are writing it to the disk. If
it is an existing file, you are simply *over*writing it to the disk.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Hard work is a medication for which
there is no placebo.

"DHarry" <DH****@discussions.microsoft.com> wrote in message
news:7E**********************************@microsof t.com...
How can I clear a text file without deleting and creating a new (empty)
file?

Apr 30 '06 #2
How can I clear a text file without deleting and creating a new (empty) file?


You can truncate a file by calling Stream.SetLength() on a FileStream.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Apr 30 '06 #3
I found a simple solution on MSDN:

using (StreamWriter sw = new StreamWriter(fileToClear))
{
sw.Write("");
}

Very simple but it did what I want to!
Apr 30 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Philip K | last post by:
Using C#, XP and 2 threads in a single App. Using filestream to open file with shared read. I have one thread writing data every 2 seconds (open, StreamWriter.WriteLine, flush, close, sleep...
2
by: Jan | last post by:
This is de public array I use, intline is a variable integer also declared as public in a module. I want to clear the content of the string array strOrderList in Form2, but i get a error in form1....
4
by: Xero | last post by:
Hello. How do you clear the content of a *.txt file? Thanks. -- Xero http://www.chezjeff.net My personal web portal
2
by: horneye | last post by:
After i use fstream to open a file, how can i use the fstream object to clear file's content?
0
by: sivachitra81 | last post by:
hi i have Richtext box.I have to add content of some RTF files and I have to add Page Break between them.And I have to append some string also to it.I am adding RTF File content by using following...
1
mahet
by: mahet | last post by:
Anders Jansson was a guest here, and he was having problem: When I try to build the web, this error is displayed: ---> Error 5 Unable to convert input xml file content to a DataSet. Invalid...
0
by: nehacredo | last post by:
hi alll.. i wanna display a word file content in a browser..how can i do that..i hav done one coding in that i can display the contents...but with contents some symbols also coming... anyone pls...
3
by: ursdhivi | last post by:
how to search the selected folder content ?
2
by: subrat2008 | last post by:
Hi I need Code that Print .pcap file content on cosole/file that should be similar with as Wire shark works. Thanks.
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.