Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

Posted
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."
Posted

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

Posted

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.

  • Leaders
Posted

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();

  • 4 years later...
Posted

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();

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...