fsetpos( ftdata, &index.pos ) ;Here I've used fsetpos() because of the type of index.pos which is fpos_t. An alternative way is to use ftell instead of fgetpos and fsek instead of fgetpos. The pair fseek and ftell work with int whereas fgetpos and fsetpos use fpos_t.
fread( text,index.size, 1, ftdata) ;
text[ index.size ]='\0';
After reading the record into memory, a null character \0 is appended to turn it into a proper c-string. Don't forget it or you'll get a crash. As before fclose is called on both files. Althogh you won't lose any data if you forget fclose (unlike with writes) you will have a memory leak.
Performance Note
Most operating systems employ buffering with files but in my experience the file buffers are not that big. If you were using this example on large files, it might be faster to store the index structs in memory and write the data file in one loop, then write all the index structs into a file.Buffering is more relevant with text files as we'll see in the next lesson.
This completes this C programming tutorial on random access files. The next one will be on text files.

