4.9.1 Adding Records

Dinsert adds records to an RMSfile. The data to be added to the file must be put in the user record prior to calling dinsert. RMS extracts the record key from the user record, moves fields from the user record to the actual file record (based on the user field list established by drlist or dblist), and stores the actual record in the RMS data file. Any fields in the file record not included in the user field list are set to zero. If there are indexes associated with the RMSfile, the key values are added to the indexes in the RMS index file. If an error occurs while trying to add the record, -1 is returned and the record is not added to the file. A sample call to dinsert might be:

recno = dinsert (&magbuf, mag);

In this example, a new magazine is added to the magazine file. The order in which records are stored and retrieved by key is not guaranteed. In most cases, multiple records are stored sequentially and retrieved in the same order. If a record is deleted, a record added later with dinsert may reuse the deleted record's slot.

Duplicate records with the same key are allowed in one RMSfile. It is up to your program to ensure that duplicates do not occur in files where this would cause problems (i.e., two magazines with the same magazine code). To do this a program might have the following code:

#include <stdio.h>

#include <cbase/dirio.h>

#include <cbase/dtypes.h>

#include "mag.h"

add_mag (magazine, title)

char *magazine;

char *title;


/* move key into data buffer */

strncpy (magbuf.magazine,

magazine, sizeof (magbuf.magazine));

if (dfindk (&magbuf, mag) > 0L) {

printf ("magazine record already exists for %s\n",


else {

/* move data into buffer */

strncpy (magbuf.magazine, magazine,

sizeof (magbuf.magazine));

/* move other fields into buffer */

/* ... */

if (dinsert (&magbuf, mag) < 0L) {

printf ("%s\ncan't add magazine record for %s\n",

derrmsg(), magazine);



printf ("magazine added: %s = %s\n",

magbuf.magazine, magbuf.title);