mhoac Posted November 12, 2003 Posted November 12, 2003 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 Quote
Administrators PlausiblyDamp Posted November 12, 2003 Administrators Posted November 12, 2003 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 file's name. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
mocella Posted November 13, 2003 Posted November 13, 2003 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." Quote
mhoac Posted November 14, 2003 Author Posted November 14, 2003 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 Quote
mocella Posted November 14, 2003 Posted November 14, 2003 I don't think you'd have to seek in Append mode, I believe it's 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); I'm 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. Quote
Leaders dynamic_sysop Posted November 14, 2003 Leaders Posted November 14, 2003 here's a quick example i knocked up for you , this is how i'd do it anyway :) .... 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 we'll 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(); Quote
ANAND_MAT Posted May 8, 2008 Posted May 8, 2008 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(); Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.