Read / Write a file

  • Thread starter Thread starter PHead
  • Start date Start date
P

PHead

I need to be able to read and write to a data file, but I dont want to
load the entire file into memory to make the change. Is it possible to
set a "record pointer" and just read and write to a certain amount of
data? I guess much like a database does. I know about filestreams,
but they wont just update one single peice of data(um) within the
stream without writing it all back to disk AFAIK. (and unfortunately
no, I cant use a database to do what Im trying to do)
 
You can use a filestream like you mentioned. Just open the file, seek
to the location you wish to write to and then write the data.

The only problem is when the amount of data you are writing is either
larger or smaller than the data that is currently there. You may have
to create a temporary file and read up to the point you need to change,
writing that to the temporary file, then write the new data to the temp
file, then read the remainder of the file and write that to the
temporary file. Finally, close the temporary file, delete the
original file and rename the temporary file. Unfortunately, there is
no other way around this...

Except if your data records in the file are all fixed length. If that
is so, you can just seek to the point that needs to change, write the
changes and close the file.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top