Download Example 2.
This shows an index and data file pair storing strings in a random access file. The strings are differing lengths and are indexed by position 0, 1 etc.
Though it would be easy to store this in one structure in a file and read it all into memory, this isn't always possible due to memory limitations.
How It Works
There are two void functions: CreateFiles() and ShowRecord(int recnum). CreateFiles uses a char * buffer of size 1100 to hold a temporary string made u of the format string msg followed by n asterisks where n varies from 5 to 1004. Two FILE * are created both using wb filemode in the variables ftindex and ftdata. After creation these are used to manipulate the files. The two files are- index.dat
- data.dat
sprintf(text,msg,i,i+5);populates the string msg like this
for ( j=0; j<i+5; j++)
strcat(text,"*");
This is string 0 followed by 5 asterisks :*****and so on. Then this
This is string 1 followed by 6 asterisks :******
index.size = (int)strlen(text) ;populates the struct with the length of the string and the point in the data file where the string will be written.
fgetpos(ftdata, &index.pos ) ;
On the next page : More on Example 2

