473,416 Members | 1,736 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,416 software developers and data experts.

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 18554
ad <ad@wfes.tcc.edu.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.com>
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.com>
???????:MP************************@msnews.microsof t.com...
ad <ad@wfes.tcc.edu.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.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 17 '05 #3
ad <ad@wfes.tcc.edu.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.com>
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(filenpath))
{
string line;
while ((line = reader.ReadLine()) != null)
{
// do whatever with the "line" to delete or add stuff
writer.WriteLine(line);
}
reader.Close();
}

writer.Close();
}
File.Delete(filepath);
File.Move(newFilePath, 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(filepath))
{
fileContents = reader.ReadToEnd();
reader.Close();
}

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

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

Also note, I have not checked to see if the file exists
(File.Exists(filename)) 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.edu.tw> wrote in message
news:OS*************@TK2MSFTNGP12.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.com>
???????:MP************************@msnews.microsof t.com...
ad <ad@wfes.tcc.edu.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.com>
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.edu.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
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...
16
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...
9
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...
6
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 =...
0
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...
2
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,...
5
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...
3
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...
2
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...
29
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...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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...
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
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...

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.