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

System.IO : Inserting a Text in between

Sam
Hi,
I have a file in which I want to insert a text in between say the a specific
line.
Basically, I want to read through the file and search for a particular line
or character and then once I find this line or character I am searching for I
want to Insert a new string in a new line below the Line I was searching for.

Any examples on how to acheive this?

Line #1
Line #2
Line #3
Line #4
Line #5
Line #6

Search for Line #3, when found Insert a new line below it to look like this.

Line #1
Line #2
Line #3
NEW LINE INSERTES!!!
Line #4
Line #5
Line #6
Aug 6 '05 #1
2 1342


Sam wrote:
Any examples on how to acheive this?


You will need to open a temporary file and write to that, then
afterwards do some replacing.

Below is some code to get you started, beware that the code have
line-ending problems and may add a newline to the end-of-file if none
exists, but it will get you started.

static void add_line(
TextReader from, TextWriter to,
string after, string add)
{
for ( string line = from.ReadLine();
line != null;
line = from.ReadLine() )
{
to.WriteLine(line);
if ( line.LastIndexOf(after) != -1 )
to.WriteLine(add);
}
}
static void add_line(string path, string after, string add)
{
string tmp_path = string.Format("{0}.tmp", path);
using ( TextWriter w = new StreamWriter(File.OpenWrite(tmp_path)))
using ( TextReader r = new StreamReader(File.OpenRead(path)))
add_line(r, w, after, add);
// This way to replace is *not* atomic,
// but i don't know any other way that works on win32
File.Delete(path);
File.Move(tmp_path, path);
}

--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Aug 6 '05 #2
If you don't want to read the entire file into memory first then I'd follow
on from Helge Jensen response above.

If you are already reading in the entire file (e.g. using
StreamReader.ReadToEnd()) then I'd just use a regular expression to find and
add the new line.

--
Brian Delahunty
Ireland

http://briandela.com/blog
"Sam" wrote:
Hi,
I have a file in which I want to insert a text in between say the a specific
line.
Basically, I want to read through the file and search for a particular line
or character and then once I find this line or character I am searching for I
want to Insert a new string in a new line below the Line I was searching for.

Any examples on how to acheive this?

Line #1
Line #2
Line #3
Line #4
Line #5
Line #6

Search for Line #3, when found Insert a new line below it to look like this.

Line #1
Line #2
Line #3
NEW LINE INSERTES!!!
Line #4
Line #5
Line #6

Aug 7 '05 #3

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

Similar topics

1
by: M Wells | last post by:
Hi All, I've been checking the online javascript libraries for code that will show me how to insert characters at the current cursor point in a textarea form field to help me markup content I...
3
by: francescomoi | last post by:
Hi. I'm trying to insert some text between <head> and <body> but I'm not able. I try with: -------- bodyPoint = document.getElementsByTagName('body'); var myLink =...
1
by: John Salerno | last post by:
If I want to create a button that adds text boxes to a form, how would I write the code so that text boxes can be inserted between text boxes that already exist? For example, if this displays...
2
by: TN Bella | last post by:
PLEASE HELP!! I am so far behind....and it doesn't help that I am learning all this stuff. I am using .net framework 1.0 from WebMatrix. These parameters get me all confused...someone please help!...
2
by: Sam | last post by:
Hi, I have a file in which I want to insert a text in between say the a specific line. Basically, I want to read through the file and search for a particular line or character and then once I...
7
by: MaxMax | last post by:
I want to insert a white line between XML elements to raise human readability example: <MyNode> </MyNode> <MyNode> </MyNode>
2
by: cesarcesar | last post by:
Hi, XML novice here with another question. I need to add a XML node like, <child01> <child02> <text></text> <image>image 1</image> <image>image 2</image>
2
by: AlexanderDeLarge | last post by:
Hi! I got a problem that's driving me crazy and I'm desperately in need of help. I'll explain my scenario: I'm doing a database driven site for a band, I got these tables for their discography...
3
by: ishanisircar | last post by:
Hi all, I am using a Gridview which displays a calendar. when i click on the calendar, the date should be inserted in my database. but instead i get this error. ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: 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
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,...
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
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...
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...

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.