HighTechTalks DotNet Forums  

System.IO : Inserting a Text in between

Dotnet General Discussions microsoft.public.dotnet.general


Discuss System.IO : Inserting a Text in between in the Dotnet General Discussions forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Sam
 
Posts: n/a

Default System.IO : Inserting a Text in between - 08-06-2005 , 06:49 AM






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

Reply With Quote
  #2  
Old   
Helge Jensen
 
Posts: n/a

Default Re: System.IO : Inserting a Text in between - 08-06-2005 , 07:53 AM








Sam wrote:

Quote:
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:helge.jensen (AT) slog (DOT) dk
sip:helge.jensen (AT) slog (DOT) dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-


Reply With Quote
  #3  
Old   
Brian Delahunty
 
Posts: n/a

Default RE: System.IO : Inserting a Text in between - 08-07-2005 , 03:37 PM



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:

Quote:
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

Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.