C Language Development Kit/Snippets/Database/GetCurrentRecord

From OptiWiki

Jump to: navigation, search
Description Get the current record number.
long GetCurrentRecord( SDBFile *dbFile );
Arguments SDBFile *dbFile

dbFile is pointer to a structure that holds information about the database. 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
-1L Current record number can not be determined.
>=0L The current record number.
Remarks The declaration of the SDBFile needs to be static to make sure all items are initialized to 0.

When the GetCurrentRecord() functions returns -1L the error code can be obtained by making a call to GetDBErrorCode() function.

The database is zero indexed, this means that the first record in the database has record number 0.

#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
    long recno;
    printf("\fOpen database\n");
    if( !CreateDatabase("file1.txt", 32, &dbFile ))
        printf("Error code = %ld\n", GetDBErrorCode());
    printf("Write a record\n");
    if( !WriteRecord(&dbFile,(char*)"123456789012345678901234567890\r\n",WRITE_APPEND))
        printf("Error code = %ld\n", GetDBErrorCode());
    if( (recno = GetCurrentRecord( &dbFile )) != -1L)
        printf("Error code = %ld\n", GetDBErrorCode());
    printf("Current rec = %ld\n", recno);
    printf("Close database\n");
    CloseDatabase( &dbFile );
    while( 1 )

Personal tools