Solved

FileStream.Write Starting and Stopping

Posted on 2008-10-18
4
782 Views
Last Modified: 2012-05-05
Is the below pseudocode possible in C#?  If so, could someone please provide a short way or explanation of how to do it?

Start FileStream.Write
Stop FileStream.Write
<Do some stuff>
Resume FileStream.Write where it left off

0
Comment
Question by:jason4659
  • 2
4 Comments
 
LVL 19

Expert Comment

by:drichards
ID: 22748924
You can do it, but how you do it depends on what you mean by "Stop FileStream.Write".  If you keep a reference to the stream around, you can keep writing as long as you haven't closed it.  If you close the stream, you need to reopen it with mode Append, and then you can keep writing to the same file.
0
 

Author Comment

by:jason4659
ID: 22748983
Sorry, I should not have said resume where I left off.  I actually need to skip a part of the file that I'm writing to and resume from there.  So I suppose it would be more like:

Start FileStream.Write
Stop FileStream.Write
Skip Ahead in File
Resume FileStream.Write
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 22749253
"Skip Ahead in File"

Use the FileStream.Seek() method to change the current position in the file:
http://msdn.microsoft.com/en-us/library/system.io.filestream.seek.aspx
0
 
LVL 19

Expert Comment

by:drichards
ID: 22754324
A couple of things to note when using seek:

When you start writing to a position in the middle of the file, it overwrites the content from that point forward.  If you want to insert, you need to first copy the data past the seek point and rewrite it after the new data.

If you write data into the middle of the file and do not pass the previous end of the file, the old data will still be there at the end.  You can use SetLength to truncate the file to the end of the desired data.
http://msdn.microsoft.com/en-us/library/system.io.filestream.setlength.aspx

One question, though.  If you start and stop writing a file, what is there to skip ahead to?  Seems like you'd be operating on an existing file for this scenario.  Or are you seeking back to the beginning at some point?

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question