PDA

You are currently viewing a search engine-friendly (archive) version of this page.

View Full Version : Read DBF Files From C


MikeFoster
April 1, 2001, 03:07 am
Have you ever needed to open and read a .dbf file from your C program? I have. If you ever have the need, let me know. Here's the header:

/*-----------------------------------------------------------------------------
cdb.h : dbf routines in C : interface
Michael Foster, June 95, August 95, September 95
-----------------------------------------------------------------------------*/


#ifndef CDB_H
#define CDB_H


#include <dos.h> /* for FP_XXX macros and getdate */
#include <alloc.h> /* for malloc and farmalloc */
#include <string.h> /* for movedata */
#include "LowLvlIO.h" /* for file i/o */


/*------------------------------- Constants ---------------------------------*/


#define HDR_REC_SIZE 32 /* size of each record in the header */

#define MAX_CACHE_SIZE 0xFFFF /* maximum size of i/o buffer */


/*---------------------------- DBF Header Types -----------------------------*/


typedef struct tagDBFTableHeader {

byte Type; /* 3 = no memo, 83h,F5h,8Bh = has memo*/
byte LastEditYear; /* year of last edit */
byte LastEditMonth; /* month of last edit */
byte LastEditDay; /* day of last edit */

} tDBFTableHeader;


typedef struct tagDBFRecordHeader {

dword Total; /* total number of records in file */
word Offset; /* byte offset to first record */
word Size; /* record size in bytes */
byte Reserved[20];

} tDBFRecordHeader;


/*---------------------------- I/O Cache Type -------------------------------*/


typedef struct tagIOCache {

word MaxCount; /* max records that will fit in cache */
word CurCount; /* num recs currently in cache(APPEND)
rec pointed to by RecPtr (READ) */
word ByteSize; /* byte size of cache */
byte far* Buffer; /* points to the i/o buffer */
byte far* RecPtr; /* current rd/write position in cache */

} tIOCache;


/*-------------------------- Buffered DBF Type ------------------------------*/


typedef struct tagDBF {

tDBFTableHeader Table; /* table info from DBF header */
tDBFRecordHeader Record; /* record info from DBF header */
tFileHandle Handle; /* low-level file handle */
tFOpenMode Mode; /* reason for opening this file */
tIOCache Cache; /* i/o buffer */
word Error; /* DOS i/o error codes */

} tDBF;


/*-------------------------- tDBF Object Macros -----------------------------*/


#define dbRecPtr( db ) ( db->Cache.RecPtr ) /* byte far* */

#define dbRecTotal( db ) ( db->Record.Total ) /* dword */

#define dbRecSize( db ) ( db->Record.Size ) /* word (includes del flg) */

#define dbError( db ) ( db->Error ) /* word */


/*----------------------- Public Function Prototypes ------------------------*/


tDBF* dbOpen( char* file, tFOpenMode mode , word cache_size );

void dbClose( tDBF* db );

tDBF* dbCopyStruc( char* file, word cache_size, tDBF* old_db );

bool dbGoto( dword rec_num, tDBF* db );

bool dbIterate( tDBF* db );

bool dbLoadCache( tDBF* db );

bool dbAppend( void far* rec, tDBF* db );

bool dbFlushCache( tDBF* db );


#endif


/* end cdb.h */


Keep Lookin Up!

------------------
Try these HelpFromTechs.com Web Development Resources:
Reference Links (http://www.helpfromtechs.com/ubb/Forum8/HTML/000054.html)*|*User Authentication (http://www.helpfromtechs.com/ubb/Forum8/HTML/000071.html)*|*HTML Editors (http://www.helpfromtechs.com/ubb/Forum8/HTML/000030.html)*|*Perl/CGI (http://www.helpfromtechs.com/ubb/Forum8/HTML/000016.html)*|*Embedding Music (http://www.helpfromtechs.com/ubb/Forum8/HTML/000067.html)

Tonker
April 2, 2001, 11:36 am
What's .dbf?

------------------

MikeFoster
April 2, 2001, 12:44 pm
.dbf is the database file format made popular by DBase. Now this format is almost a defacto standard, and is used by many different applications.

------------------
Try these HelpFromTechs.com Web Development Resources:
Reference Links (http://www.helpfromtechs.com/ubb/Forum8/HTML/000054.html)*|*User Authentication (http://www.helpfromtechs.com/ubb/Forum8/HTML/000071.html)*|*HTML Editors (http://www.helpfromtechs.com/ubb/Forum8/HTML/000030.html)*|*Perl/CGI (http://www.helpfromtechs.com/ubb/Forum8/HTML/000016.html)*|*Embedding Music (http://www.helpfromtechs.com/ubb/Forum8/HTML/000067.html)

MikeFoster
July 14, 2001, 10:07 am
Boy! The response to this topic took me by surprise. Many people have emailed me asking for C code that allows access to .dbf files. So I finally uploaded cdbh.zip (http://lineoflight.com/download/cdbh.zip) to one of my sites. I hope its useful to you, thank you for your interest!

BTW, I enjoy your emails, but I just don't have time to provide free email support any more. Please keep the discussions here at the forum - so everyone can participate.

Thanks a bunch!
Mike

------------------
Help From Techs Support Forums