473,938 Members | 1,642 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Clear the content of a file

Hello.
How do you clear the content of a *.txt file?
Thanks.

--
Xero

http://www.chezjeff.net
My personal web portal
Nov 21 '05 #1
4 7473
"Xero" <jeff_@_chezjef f_._net(remove_ underscores_and _this)> wrote in message
news:43******** *************** ***********@mic rosoft.com...
How do you clear the content of a *.txt file?


System.IO.File. Open( "foo.xml", System.IO.FileM ode.Truncate).C lose

Bit of an audacious one-liner, but you get the drift. Open the file in truncate
mode, then (as there's nothing really to do in truncate mode after having reset
the file to zero-length) close it.

Opening it with FileMode.Create will also achieve the same thing, and allow
you to use the FileStream afterwards, for instance,

Dim fileStream As System.IO.FileS tream
fileStream = System.IO.File. Open( "foo.xml", System.IO.FileM ode.Create)
'
' foo.xml is now zero-length, plus use can write to fileStream.
'
fileStream.Flus h
fileStream.Clos e
Derek Harmon
Nov 21 '05 #2
Jeff,

Derek gave you the answer, however what he did not write in my opinon in
words was.

"Just create an empty one with the same name". (And than use one of his
samples)

Only meant as addition

Cor

"Xero" <jeff_@_chezjef f_._net(remove_ underscores_and _this)>
Hello.
How do you clear the content of a *.txt file?
Thanks.

Nov 21 '05 #3
I see.
Thanks very much, guys.

Xero
"Cor Ligthert" wrote:
Jeff,

Derek gave you the answer, however what he did not write in my opinon in
words was.

"Just create an empty one with the same name". (And than use one of his
samples)

Only meant as addition

Cor

"Xero" <jeff_@_chezjef f_._net(remove_ underscores_and _this)>
Hello.
How do you clear the content of a *.txt file?
Thanks.


Nov 21 '05 #4
"Xero" <jeff_@_chezjef f_._net(remove_ underscores_and _this)> schrieb:
How do you clear the content of a *.txt file?


Open the file using 'FileStream' and use its 'SetLength' method to reset the
file's length.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #5

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

Similar topics

2
23129
by: Jan | last post by:
I'm busy making a very basic and simple content management system wich stores the text in a .txt file. now i need to know how i can clear the ..txt file.. now it just adds the new text to the .txt file while it should replace it! I also like to display the old text in the textarea... help me please!
2
38180
by: Sure | last post by:
Hello Group, I have a php file which will list the pending task for each developer. If the developer changes some thing it will update to database. Once the user clicks the update button it shows the another page and there will be button back to list of pending task. It will again fetch from database. At the second time if user update some other data. It giving
4
1765
by: Christian Otteneuer | last post by:
Hello Newsgroup, I have the following problem: I want to write a python script, that writes text into files. The text is being "collected" in a for-loop, the code looks like this: for i in range(len(myList)): id = myList print "some text" container.writeToFile(id, printed)
3
11165
by: Kyle Kolander | last post by:
I posted this in response to a previous thread but have not gotten any replies back... Hopefully someone has an answer? From looking at sections 27.7.3.2 and 27.7.1.2 of the standard, it appears str("") does not clear the stream bits. It seems to me that in clearing the internal buffer, one would intend that the stream bits would also be reset (in a good state). Is there a reason this is not the defined behavior? Aside from a slight...
4
2255
by: aikwee | last post by:
how, how do i clear the current content of a datagrid? I am using datagrid to do a xml editor, so every time i try to open anther file and load it to datagrid, it actually append it in to previous contents... please advice...
4
10617
by: Dhans | last post by:
Hi there, I am using log4net in my (.net v1.1) application to log the messages. I tried to clear the logfile while application is running. since log4net has the file handle it did not allow me to clear/delete the file. I need to clear the content of the log file specified in the log4net configuration file, when it is needed in the application(such as some button click event), If anybody familiar with log4net your help would be...
3
15997
by: DHarry | last post by:
How can I clear a text file without deleting and creating a new (empty) file?
2
8664
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?
3
5958
by: ffrugone | last post by:
I am creating a blog and I have a problem which seems fairly common: Within the content of the posts I sometimes have floated images. The comments bar that follows each post will not clear the float unless I use clear:both in the class for the comment bar. HOWEVER, when clear:both is used for the comment bar, the first post on the blog shows a huge, (enormous) vertical gap between the content and the comment bar when viewed using...
0
9963
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11516
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
11103
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...
0
9854
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...
1
8214
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7378
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();...
0
6072
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6290
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4444
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.