C programming question – flat file database
For my X Windows assignment, part of the project was to create a “database” from a flat file and an index file. The flat file had to be an ASCII file in the following format:
Field1: value Field2: value Description: description value, variable length field . PrimaryKey: key String
As you can see, the field names are included on each line and the description field can be of any length, with the end being delimited by the period.
My question is this; what is the best way of actually programming this? The way that I did it was to create a struct that contained the data from each field. I limited the length of each field to about 45 characters and the length of the description field to 2000. However, this still means reading in each line using fgets(), stripping out the field name, and strcpy() the data into the corresponding field in the struct. For the description field, I looped until I got a string that was equal to ".\n".
For the index file, I simply read through each record in the file, noted its record count and its search key in an index and sorted the index. This gave me how many records I had to read through in order to get to the one I wanted.
For a small data file this works fine, but due to the lack of random access, it would become hideous for large files. There has to be a better way of managing and programming a data file like this. The problem is that I cannot think of one. Anyone out there have any suggestions?