C Language Development Kit/Snippets/Database/AddNewSearchkeyToIndex

From OptiWiki

Jump to: navigation, search
Description Add a new search key item to the index database.
Syntax
int AddNewSearchkeyToIndex( SDBFile *dbFile, char *nwsearchkey, long recordnumber );
Arguments SDBFile *dbIndex

dbIndex is pointer to a structure that holds information about the index file. The SDBFile structure is defined as followed:

typedef struct
{
    short  sRecSz;        // record size of database
    int    fd;            // real handle to the open file
    long   lCurrRecord;   // current record number
    long   lTotalRecords; // total amount of records
    int    bOpen;         // check to see if db is open or closed
}SDBFile;
char *nwsearchkey

The new searchkey to add to the database.

long recordnumber

The record number where the key is pointing to in the original database.

Returns
TRUE On success.
FLASE On failure.
Remarks The declaration of the SDBFile needs to be static to make sure all items are initialized to 0. When the AddNewSearchkeyToIndex() functions returns FALSE the error code can be obtained by making a call to GetDBErrorCode() function.
Example
#include "lib.h"
#include <stdio.h>
#include "database.h"  //Holds the database functions
 
void main( void )
{
    static SDBFile dbFile;  // static initializes all items to 0
    static SDBFile dbIndex; // static initializes all items to 0
    long recno;
    int i;
    char record[32 + 1];
 
    printf("\fCreate database\n");
    if( !CreateDatabase("file1.txt", 32, &dbFile ))
        printf("Error code = %ld\n", GetDBErrorCode());
 
    memset( record, '\0', sizeof( record ));
    printf("Write 10 records\n");
    for( i = 9; i > -1; i-- )
    {
        sprintf(record, "%01d23456789012345678901234567890\r\n", i);
        if( !WriteRecord( &dbFile, record, WRITE_APPEND ))
        {
            printf("Error code = %ld\n", GetDBErrorCode());
            break;
        }
    }
 
    if( !CreateIndexFile( &dbFile, 0, 2, (char*)"file_idx.txt", &dbIndex ))
        printf("Error code = %ld\n", GetDBErrorCode());
 
    if( (recno = SearchIndexFile( &dbIndex,(char*)"62")) == -1L)
        printf("Error code = %ld\n", GetDBErrorCode());
 
    if( !GotoRecord( &dbFile, recno ))
        printf("Error code = %ld\n", GetDBErrorCode());
    if( !ReadCurrentRecord( &dbFile, record ))
        printf("Error code = %ld\n", GetDBErrorCode());
    printf("recnr = %ld, %-30.30s\n", recno, record );
 
    printf("Close Index file\n");
    CloseDatabase( &dbIndex );
 
    if(!WriteRecord(&dbFile,(char*)"593456789012345678901234567890\r\n",WRITE_APPEND))
        printf("Error code = %ld\n", GetDBErrorCode());
 
    printf("Open Index file\n");
    if( !OpenIndexFile( (char*)"file_idx.txt", 2, &dbIndex ))
        printf("Error code = %ld\n", GetDBErrorCode());
 
    if( !AddNewSearchkeyToIndex( &dbIndex, (char*)"59", GetCurrentRecord( &dbFile )))
        printf("Error code = %ld\n", GetDBErrorCode());
 
    if( (recno = SearchIndexFile( &dbIndex,(char*)"59")) == -1L)
        printf("Error code = %ld\n", GetDBErrorCode());
 
    if( !GotoRecord( &dbFile, recno ))
        printf("Error code = %ld\n", GetDBErrorCode());
    if( !ReadCurrentRecord( &dbFile, record ))
        printf("Error code = %ld\n", GetDBErrorCode());
    printf("recnr = %ld, %-30.30s\n", recno, record );
 
    printf("Close Index file\n");
    CloseDatabase( &dbIndex );
 
    printf("Close database\n");
    CloseDatabase( &dbFile );
 
    while( 1 )
        idle();
}


Views
Personal tools