er ... how do I open a exsit file and write extra string into any line(s) in the original file
like say the original file is
ABCDHIJKLMN...
and i wanna insert EFG into ABCD and HIJ...
what should i do? :s
Printable View
er ... how do I open a exsit file and write extra string into any line(s) in the original file
like say the original file is
ABCDHIJKLMN...
and i wanna insert EFG into ABCD and HIJ...
what should i do? :s
I haven't done this, but you are going to want to look into streams. Maybe TextWriter and TextReader streams?..?
Not sure, good luck.
I played around for you, and I couldn't get it to insert data, just overwrite...
Code:// C:\text.txt contains "ABCDEFGMNOPQRSTUVWXYZ"
FileStream f = new FileStream("C:\\text.txt", FileMode.Open , FileAccess.ReadWrite);
while(f.ReadByte() != 'G')
{
}
f.WriteByte((byte)'H');
f.WriteByte((byte)'I');
f.WriteByte((byte)'J');
f.WriteByte((byte)'K');
f.WriteByte((byte)'L');
f.Close();
// C:\text.txt contains "ABCDEFGHIJKLRSTUVWXYZ"
I fear it can't be done. You must read in everything after the location where you want to insert, then write out what you want to insert and then the rest of the file.