I need to store set of key-value pairs in a file. values are name of persons and keys should be auto generated and are hidden to user (like auto-number in database but I do not need DB) how should I do this?
+1
A:
If the file is entirely under your control and only ever written to from a single process at a time, this is easy: you just keep an object containing the "current key" and increment it every time your write.
An alternative to autonumbering is to use a Guid
, which means you don't need to keep any state - just generate a new Guid
for every record.
Was this the kind of thing you meant, or are you effectively asking for help designing the file format? If it's the latter, you might want to think about:
- Do you need an on-disk index?
- Can you keep the entire file in-memory if you need to query it?
- Would you benefit from fixed-width fields? (If so, you should think about a fixed-width encoding as well.)
- Do you need it to be human-readable?
- Are you more interested in space or time efficiency? Is efficiency particularly important for you in the first place?
Jon Skeet
2009-09-05 08:16:02