473,804 Members | 2,184 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to delete some text in a file

ad
I want to delete some text which begin with <!DOCTYPE and end with > in a
file.
How can I do it?
Nov 17 '05 #1
5 18587
ad <ad@wfes.tcc.ed u.tw> wrote:
I want to delete some text which begin with <!DOCTYPE and end with > in a
file.
How can I do it?


You'll need to rewrite the file. You could do it by reading from the
original file, writing (as you go) everything you *do* want to a new
file, and then moving it over the top of the old one. Alternatively you
could read the whole of the old file into memory, then write out just
the bits you want over the top of the old file.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2
ad
Thanks,
How could I open a file, and read the content line by line?
Could you give me an example?

"Jon Skeet [C# MVP]" <sk***@pobox.co m>
???????:MP***** *************** ****@msnews.mic rosoft.com...
ad <ad@wfes.tcc.ed u.tw> wrote:
I want to delete some text which begin with <!DOCTYPE and end with > in a file.
How can I do it?


You'll need to rewrite the file. You could do it by reading from the
original file, writing (as you go) everything you *do* want to a new
file, and then moving it over the top of the old one. Alternatively you
could read the whole of the old file into memory, then write out just
the bits you want over the top of the old file.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 17 '05 #3
ad <ad@wfes.tcc.ed u.tw> wrote:
How could I open a file, and read the content line by line?
Could you give me an example?


Use StreamReader and its ReadLine method. See the docs for ReadLine for
an example, but rather than using Peek, call ReadLine until it returns
null.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #4
ad,

Before I start: Code disclaimer: The code below overwrites/deletes data. I
have not tested it. It is purely meant as an example, and no warrenties are
either expressed or implied. No usefulness statement is made. Use at your
own risk.

One approach would be to read line by line as you mentioned:

string filepath = "myfilename.txt ";
string newFilePath = "myNewFile.txt" ;
using (TextWriter writer = File.CreateText (newFilePath))
{
using (TextReader reader = File.OpenText(f ilenpath))
{
string line;
while ((line = reader.ReadLine ()) != null)
{
// do whatever with the "line" to delete or add stuff
writer.WriteLin e(line);
}
reader.Close();
}

writer.Close();
}
File.Delete(fil epath);
File.Move(newFi lePath, filepath);

A better approach might be to read the entire file into a string, manipulate
that string, and then write the whole string back. For example:

string filepath = "myfilename.txt ";
string fileContents;
using (TextReader reader = File.OpenText(f ilepath))
{
fileContents = reader.ReadToEn d();
reader.Close();
}

// use fileContents.Re place() or other string manipulation to fix the file

using (TextWriter writer = File.CreateText (filepath))
{
writer.Write(fi leContents);
writer.Close();
}

Also note, I have not checked to see if the file exists
(File.Exists(fi lename)) or done any other error checking. This is just an
example for you to get started with.

Hope this helps...

--
Frisky

Intellectuals solve problems; geniuses prevent them. ~ Albert Einstein
"ad" <ad@wfes.tcc.ed u.tw> wrote in message
news:OS******** *****@TK2MSFTNG P12.phx.gbl...
Thanks,
How could I open a file, and read the content line by line?
Could you give me an example?

"Jon Skeet [C# MVP]" <sk***@pobox.co m>
???????:MP***** *************** ****@msnews.mic rosoft.com...
ad <ad@wfes.tcc.ed u.tw> wrote:
> I want to delete some text which begin with <!DOCTYPE and end with > in a > file.
> How can I do it?


You'll need to rewrite the file. You could do it by reading from the
original file, writing (as you go) everything you *do* want to a new
file, and then moving it over the top of the old one. Alternatively you
could read the whole of the old file into memory, then write out just
the bits you want over the top of the old file.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Nov 17 '05 #5
On Sun, 12 Jun 2005 20:29:45 +0800, "ad" <ad@wfes.tcc.ed u.tw> wrote:
I want to delete some text which begin with <!DOCTYPE and end with > in a
file.
How can I do it?


You cannot change the file in-place if that is what you are asking.
You will have to open the file and read through it, copying the stuff
you want to keep to a second file.

rossum

The ultimate truth is that there is no ultimate truth
Nov 17 '05 #6

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

Similar topics

0
1926
by: Silvia | last post by:
Hi, I have a program that capture images and put this into a listview (using imagelist), the problem is when I delete de image the listview, when do that and capture another image, the image painting in the listview is the image delete, I supose that the problem is that the image exist in the imagelist, in order to solve this problem I clear the imagelist and add all the images captured, but i have another problem now, some time when I...
16
17026
by: Philip Boonzaaier | last post by:
I want to be able to generate SQL statements that will go through a list of data, effectively row by row, enquire on the database if this exists in the selected table- If it exists, then the colums must be UPDATED, if not, they must be INSERTED. Logically then, I would like to SELECT * FROM <TABLE> WHERE ....<Values entered here>, and then IF FOUND UPDATE <TABLE> SET .... <Values entered here> ELSE INSERT INTO <TABLE> VALUES <Values...
9
10673
by: Robert Schneider | last post by:
Hi to all, I don't understand that: I try to delete a record via JDBC. But I always get the error SQL7008 with the error code 3. It seems that this has something to do with journaling, since the table from which I want to delete has two foreign keys that references two other tables and it is also referenced by another table. But this shouldn't be a problem, since I set the commit mode to none (or *none) at all places where this makes...
6
6718
by: Poppy | last post by:
I use the following code to append a line of text to a text file : Dim myFILENAME As String = Server.MapPath("/ABACUS/cvdocs/" & fileName) Dim objStreamWriter As StreamWriter objStreamWriter = File.AppendText(myFILENAME) objStreamWriter.WriteLine(csvline) objStreamWriter.Close() The code works fine but I need to be a ble to delete the entire contents of the file as well and I cant figure it out.
0
1841
by: Silvia | last post by:
Hi, I have a program that capture images and put this into a listview (using imagelist), the problem is when I delete de image the listview, when do that and capture another image, the image painting in the listview is the image delete, I supose that the problem is that the image exist in the imagelist, in order to solve this problem I clear the imagelist and add all the images captured, but i have another problem now, some time when I...
2
4603
by: richardkreidl | last post by:
I want to be able to delete and search for elements in a XML file, I'm using the code below for adding elements which works great: Public Sub cmdAddElement_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAddElement.Click Dim doc As New XmlDocument doc.Load("c:UMZ.xml") Dim root As XmlNode = doc.DocumentElement
5
39060
by: wo20051223 | last post by:
Deleting some files with C# fails with "Access to the path 'X' is denied". I have files copied from a CD that I burned (and not locked by a process) and a text file that I created in Windows Explorer. I can delete all of them through Windows Explorer. I can programmatically delete the text file but not the others. Permissions: - All files have the same ACL.
3
3285
by: Arpan | last post by:
A Form has a FileUpload, 2 Buttons & a TextBox web server controls. Using the FileUpload control, I want to give users the provision to move & delete files that DO NOT exist in C:\Inetpub\wwwroot (i.e. the root directory). This is the code: <script runat="server"> Sub MoveFile(ByVal obj As Object, ByVal ea As EventArgs) File.Move(fudFileSource.FileName, txtFileDest.Text) 'File.Move("F:\4.jpg", "C:\4.jpg") End Sub
2
3542
by: cdun2 | last post by:
Hello, I have some code that reads each line of a text file, and if a line is found where the length of the string in the line is 384, it writes the line to a text file. The other step that I need to take is to delete the line from the source file. The code is as follows; -***************************************** Public Sub Main()
29
5351
by: shivasusan | last post by:
Hi! I can add rows with inputs to my HTML table dynamically using DOM, but I cannot remove selected rows. In fact, every row contains a Delete button. So, user selects the rows to remove, clicks the delete button selected row wants to delete. My problem is(If i delete the first row, its say undefined and change the value is 0) pls check my program. <%pathdefiner = "../"%> <!--#include file="../connection/connector.asp" -->...
0
9712
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
10595
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
10089
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...
1
7634
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
6862
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
5530
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
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4308
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
3
3001
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.