473,830 Members | 2,091 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 15914
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****@discuss ions.microsoft. com> wrote in message
news:7E******** *************** ***********@mic rosoft.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.SetLengt h() 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(fi leToClear))
{
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
987
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 2sec) Other thread reading every 5 seconds (open, StreamReader.ReadLines while Length > position, close) However while the read thread is actually run every 5 seconds, it does not
2
5805
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. How can i do this?? strOrderList.Clear(?, 0, intLine)
4
7467
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
8644
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
1117
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 line Richtextbox.RTF=File content; and Then If I add any other string or New line (\n) to that with "Richtextbox.Text" Property Then RTF File content text will be loosing it's Font. and I am not able to add "Page Break" also please Help me
1
4191
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 'name' attribute value 'Företag': 'The '¶' character, hexadecimal value 0xB6, at position 2 within the name, cannot be included in a name.'. <start path>My Webs\ProAra\wwwroot\App_Code\kappsegling\ProAra.Ka ppSegling.rorsmandataset.xsd -->
0
1673
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 help me my code... <%@ page import="java.io.*,javax.servlet.http.HttpServletRequest,javax.servlet.ServletInputStream" %> <%@ page import="java.io.FileWriter,java.io.IOException" %> <html> <head> <title>
3
1506
by: ursdhivi | last post by:
how to search the selected folder content ?
2
3588
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
9781
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10477
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10522
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10197
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9310
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6944
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4408
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3956
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3072
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.