Jump to content
  • Advertisement
Sign in to follow this  
MrSplosion

Can't Seek Over Pre-existing Data?

This topic is 2485 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I simply want to move a pointer to the begging of a file then start writing text to it without deleting what already there. The problem is that I get an "IOException: Can't seek back over pre-existing data in append mode" error. Here is my script it's in JavaScript but you can easily tell what I'm doing from any other language.


function WriteToFile(){
var file = StreamWriter(Application.dataPath + "/data/saveWorld.txt", true);

file.BaseStream.Seek(0, SeekOrigin.Begin);
file.WriteLine("bla bla");

file.Close();
}



Like I said before all I'm doing is moving the pointer then trying to write a line to the file. What's wrong and how can I fix it?

Thanks!

Share this post


Link to post
Share on other sites
Advertisement
The key part of that error message is "in append mode". Obviously you can't prepend data if you opened the file in append mode. I don't know JavaScript, but that looks an awful lot like System.IO.StreamWriter, which apparently has options only to overwrite or append - which kind of makes sense, if you think of the stream abstraction.

Share this post


Link to post
Share on other sites

The key part of that error message is "in append mode". Obviously you can't prepend data if you opened the file in append mode. I don't know JavaScript, but that looks an awful lot like System.IO.StreamWriter, which apparently has options only to overwrite or append - which kind of makes sense, if you think of the stream abstraction.

I can't prepend data? So there is no way to simply move the pointer somewhere and start writing without deleting whats after the pointer? I can only do it in some other mode other than append mode which will deleting whats after the pointer?

And yes I'm using System.IO.StreamWriter.

Share this post


Link to post
Share on other sites
I can't prepend data? So there is no way to simply move the pointer somewhere and start writing without deleting whats after the pointer? I can only do it in some other mode other than append mode which will deleting whats after the pointer?


Yes: using StreamWriter your options are to append-only (no seeking back beyond the end of the data!) or overwrite-only. How about reading the file in advance and then writing your data in overwrite mode followed by the pre-existing data? Or write your new data to a temp file, then use FileStream.CopyTo to append the data from the original file and File.Replace to finally replace the original file with the complete new data.

Or maybe you can use a naked FileStream and seek in that? You'd need to handle the encoding yourself.

Share this post


Link to post
Share on other sites
Files aren't really designed for the kind of writing you're thinking of doing. (It usually wouldn't work right, anyway: what if the data you're writing isn't the same length as the data that you want overwritten?)

Read the existing data, assemble the desired resulting file contents and write the entire resulting file. (You can generally calculate on the fly as you write, depending on the logic.)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!