473,495 Members | 2,021 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 1345


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
2913
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
2273
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
1259
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
3045
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
209
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
15226
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
1614
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
3056
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
2278
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
7120
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
6991
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...
0
7160
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,...
1
6878
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
7373
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
5456
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
3088
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...
0
1405
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 ...
1
649
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.