-4

I'm considering writing a file I/O abstraction. Is there support in the APIs of the common desktop operating systems for inserting or erasing from random positions in a file? Or, if offered by my interface, would I have to manually shuffle down every byte in the file?

0

3 Answers 3

0

Yes,you will need to implement shuffle by yourself.

0

Don't know about APIs per se. But Memory mapped files on Windows and Linux offer this facility in a round about way. So you could put wrappers around that.

1
  • I have looked at MMFs but it appears that I would still have to manually shuffle each byte down myself.
    – Puppy
    Jun 9, 2012 at 6:01
0

Using the C standard library, you can open a file with write permission, then call fseek() to move to specific position, and then write at that position. See the manual page for fseek(), or the web page for it: http://linux.die.net/man/3/fseek

But overwriting is all you get. If you want to insert or remove data, you will need to rewrite the whole file from that point. This is not efficient.

So, instead of using a file, use some sort of database. You can insert records and delete records without needing to rewrite the file: you just rewrite some indices. But then to read the data out, you need to use the database to walk over the indices and pull the records in the correct order.

On PalmOS, I believe the only storage available to apps was database storage! But in most environments there is no database built-in; you would need to install something.

I recommend you start out by playing with SQLite, as it is public domain (completely free for any use) and very well debugged. Its performance won't be amazing, but it will be better than completely rewriting files to insert and delete stuff. Also, since SQLite is very popular, bindings are available for most programming languages.

http://sqlite.org/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.