How to access user records stored in a file (no database)
How can the records of a user be kept and acc开发者_高级运维essed based on a primary key inside a file, working with ansi c++. A database is not used.
The most simplistic scenario would be to (ab)use the filesystem as the database. The primary key would be the name of the file and the content of the file is the record itself. Opening a file by its name/key is the lookup.
However, you may like to specify your requirements in more detail. Like what sort of records and keys, how many records it should store, how often updates and queries happen.
Whether you call it a database or not, if you are randomly accessing records in a file its a database.
Anyway, define a maximum record length for your file. Call it MAX_RECL
Open for read/write To read, seek to rec*MAX_RECL and read MAX_RECL. To write, seek to rec*MAX_RECL and write MAX_RECL. To close, seek to end and close.
To use a key, you will need a separate index that maps keys to records, unless you want to search the whole file every time.
Check embedded databases like SQLite or Berkeley DB before rolling your own one.
You can use fixed record size, or if it is variable a separate index file for the start and length of the entries.
精彩评论