Add context to file

mhoac

New member
Joined
Nov 12, 2003
Messages
2
Hi, I want to add text to an existing file. Note that I want to add it in different locations of the file, not just appending at the end.

I have tried the following in C#:
-----------------------------
FileStream myfile = new FileStream(myinputfile, FileMode.Open, FileAccess.ReadWrite);
StreamWriter sw = new StreamWriter(myfile);

sw.BaseStream.Seek(0,SeekOrigin.Begin);
sw.WriteLine("Some test");

sw.Flush();
sw.BaseStream.Seek(0, SeekOrigin.End);
sw.WriteLine("some other text");

sw.Flush();
sw.Close();
outfile.Close();
-----------------------------

Everything works fine except the writing to the beginning of file overwrites the existing contents.

My question is: How can add text to the file without overwriting existing contents? As I said earlier, I need to be able to add it anywhere in the file, not just at the end.

Thanks!
/Minh
 
Probably have to create a new temporary file and write out the new bits and copy the old bits in the correct order, delete the original and rename the new one to the old files name.
 
Have you tried it with FileMode.append when you instantiate your FileStream? In threory, that should "Opens the file if it exists and seeks to the end of the file, or creates a new file. FileMode.Append can only be used in conjunction with FileAccess.Write. Any attempt to read fails and throws an ArgumentException."
 
Yes, but when opening in Append mode and I perform Seek, then I get the following IOException:
Unhandled Exception: System.IO.IOException: Unable seek backward to overwrite data that previously existed in a file opened in Append mode.

/Minh
 
I dont think youd have to seek in Append mode, I believe its done implicitly (not positive though) by telling the system you are going to append.

Try removing and re-run your code:
sw.BaseStream.Seek(0,SeekOrigin.Begin);

Im not real experienced with this sort of operation, but perhaps you could search the rest of the site or google groups for FileMode.Append and see what you come up with.
 
heres a quick example i knocked up for you , this is how id do it anyway :) ....
C#:
			System.IO.StreamReader sr=new System.IO.StreamReader(new System.IO.FileStream(@"C:\test.txt",System.IO.FileMode.OpenOrCreate,System.IO.FileAccess.ReadWrite));
			
            StringBuilder sb=new StringBuilder();
			sb.Append(sr.ReadToEnd().ToCharArray());
			int x=sb.ToString().IndexOf("some text") + "some text".Length; /// this is where well be inserting the string.
			sb.Insert(x," TEST12345! ");
            sr.Close(); /// close the streamreader to allow access to the streamwriter.

			System.IO.StreamWriter sw=new System.IO.StreamWriter(@"C:\test.txt",false);
            sw.Write(sb.ToString());
			sw.Close();
 
This is very old post but someone may be looking for the answer.
Problem in the original code is FileMode and FileAccess
Try This (Hope this helps.):
FileStream myfile = new FileStream(myinputfile, FileMode.Append, FileAccess.Write);
StreamWriter sw = new StreamWriter(myfile);

sw.BaseStream.Seek(0,SeekOrigin.Begin);
sw.WriteLine("Some test");

sw.Flush();
sw.BaseStream.Seek(0, SeekOrigin.End);
sw.WriteLine("some other text");

sw.Flush();
sw.Close();
myfile.Close();
 
Back
Top