开发者

Access a specific record using lseek

Is it possible to access a specific one (开发者_开发知识库using a specific index) in a text file without knowing the size of each record?


If you maintain a separate index of record offsets then you can simply consult it for the appropriate location to seek to. Otherwise, no.


If the records happened to be sorted on a convenient key and you can identify where one record ends and another begins, then you can implement a binary or interpolation search approach. You may be able to add this to your text-file format retrospectively to aid the lookup. Otherwise, you're stuck with serial searches from a position with known index (obviously start of file is one, if you know the total number of records you can work backwards from end of file). You can also consider doing one pass to create an index to allow direct access, or having the file embed a list of offsets that can be easily read.


Check out the dbopen() function. If you pass DB_RECNO as the type parameter you can access variable-length records. These records can be delimited by newlines. Essentially your "database" is a flat text file.

The API will conveniently handle inserts and deletes for you as well.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜