Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 132,354 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,139 people online right now. Registration is fast and FREE... Join Now!




Writing data at a specified position in a file

 
Reply to this topicStart new topic

Writing data at a specified position in a file

jacobjordan
post 31 Aug, 2008 - 06:28 AM
Post #1


class Me : Perfection

Group Icon
Joined: 11 Jun, 2008
Posts: 1,105



Thanked 22 times

Dream Kudos: 1575
My Contributions


I am using the IO.StreamWriter class to try to make something to write data inside a file. Here is what i have
csharp

public static void EncodeData(string Data, string Path, long Index)
{
StreamWriter stream = new StreamWriter(Path, true);
stream.BaseStream.Position = Index;
stream.Write(Data);
stream.Close();
}

However, that will throw an exception that says "Unable seek backward to overwrite data that previously existed in a file opened in Append mode.". If i open the file so it's not in append mode, it will write the data at the specified index, but will delete everything else in the file. Note i want this to insert the data at the specified position, without overriding any data.
User is offlineProfile CardPM

Go to the top of the page

Martyr2
post 31 Aug, 2008 - 08:13 AM
Post #2


Programming Theoretician

Group Icon
Joined: 18 Apr, 2007
Posts: 5,027



Thanked 173 times

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions


Well there is no real easy way to insert text right in the middle of a line. I think perhaps Microsoft really dropped the ball on this one. The solutions for this problem are all over the board from reading the entire file in, manipulate the text and then write it all back out to file to transposing by putting both a Streamreader as well as a Streamwriter on the same file and seeking to the position, then reading and writing at the same time.

If you want to try the transpose method you can read the article at the URL...

CodeProject: Insert Text into Existing Files in C#

Or you can read in the entire file contents (into a buffer or array of some sort), add in the text, then write back out the file. This would be an option if the file isn't expected to be very big since this could take time.

There are two solutions for you. smile.gif
User is offlineProfile CardPM

Go to the top of the page

jacobjordan
post 31 Aug, 2008 - 03:31 PM
Post #3


class Me : Perfection

Group Icon
Joined: 11 Jun, 2008
Posts: 1,105



Thanked 22 times

Dream Kudos: 1575
My Contributions


Actually, the file is expected to be quite large; possibly in excess of 1GB. I will look at your link.

Nice to have my thread back. wink2.gif
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/22/08 04:07AM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month