C# Help - Inserting text

hx009

Senior member
Nov 26, 1999
989
0
0
Does anyone have a quick code example for inserting text into the middle of a text file? I've tried everything I've found on google with no luck. Most people say do something like the following:

// get the file contents from our position to end of file
string fileBottom;
StreamReader sr = new StreamReader("c:\\temp\\test.txt");
sr.BaseStream.Seek(5, SeekOrigin.Begin);
fileBottom = sr.ReadToEnd();
sr.Close();

// open the file, seek to our position, append our text, append bottom of file
StreamWriter sw = new StreamWriter("c:\\temp\\test.txt");
sw.BaseStream.Seek(5, SeekOrigin.Begin);
sw.Write("MYTEXT");
sw.Write(fileBottom);
sw.Close();

The only problem with this is that the resulting file comes out completely blank up to the seek point, then "MYTEXT", then the remainder of the file. What am I missing here?
 

UCJefe

Senior member
Jan 27, 2000
302
0
0
This works for me.

Edit: I lied, I mis-read your question. My previous code overwrote a few of the bytes. You wanted to insert and not overwrite. Corrected code below.
 

UCJefe

Senior member
Jan 27, 2000
302
0
0
And by the way, put using blocks around your StreamReader's and StreamWriters so they are Disposed properly when you're done with them. I should have included that in my original sample but got lazy.
 

hx009

Senior member
Nov 26, 1999
989
0
0
UCJefe's code seemed to do the trick. However, I have a question. When using the StreamReader's BaseStream.Position, what on earth is it returning? I was parsing through a file using StreamReader.ReadLine() and asked for the BaseStream.Position and it returned 323. The file doesnt have 323 characters/bytes, nor 323 lines. Using that as a seek position is causing odd results.
 

UCJefe

Senior member
Jan 27, 2000
302
0
0
Originally posted by: hx009
UCJefe's code seemed to do the trick. However, I have a question. When using the StreamReader's BaseStream.Position, what on earth is it returning? I was parsing through a file using StreamReader.ReadLine() and asked for the BaseStream.Position and it returned 323. The file doesnt have 323 characters/bytes, nor 323 lines. Using that as a seek position is causing odd results.


Well I think it's quite obvious. The MSDN doc says it returns "The current position within the stream." :) Thank you master of the obvious.

I would expect this to be bytes. How is your file encoded? Unicode? There could be a couple (or more depending on encoding) bytes per char which might account for the discrepancy. How many bytes do you think your file is?
 

hx009

Senior member
Nov 26, 1999
989
0
0
Originally posted by: UCJefe
Originally posted by: hx009
UCJefe's code seemed to do the trick. However, I have a question. When using the StreamReader's BaseStream.Position, what on earth is it returning? I was parsing through a file using StreamReader.ReadLine() and asked for the BaseStream.Position and it returned 323. The file doesnt have 323 characters/bytes, nor 323 lines. Using that as a seek position is causing odd results.


Well I think it's quite obvious. The MSDN doc says it returns "The current position within the stream." :) Thank you master of the obvious.

I would expect this to be bytes. How is your file encoded? Unicode? There could be a couple (or more depending on encoding) bytes per char which might account for the discrepancy. How many bytes do you think your file is?

Well considering there are two ways to view "position". One is the actual bytes "traversed" (which is what the position property returns), versus offset location, which is what the Seek() method uses. I was asking the system for the position of some certain text, then trying to Seek() to that position later. Apples and oranges. I fixed it by recording the position, then just manually setting the position in the stream later. Works like a champ!

Thanks for your help though.