Some c-code to access a dbf.
In the example any dbf passed as parameter will be listed.
Please adjust the code to your own needs.
This is MSC-6 code.
The nandef.h header file you should find it with your clipper compiler.
dbf.h header file
/***
* dbf.h
*/
typedef struct
{
char dbf_id;
char last_update[3];
long last_rec; /* unsigned ?? */
unsigned data_offset;
unsigned rec_size;
char filler[20];
} DBF_HEAD;
typedef struct
{
char field_name[11];
char field_type;
char dummy[4];
union
{
unsigned char_len;
struct
{
char len;
char dec;
} num_size;
} len_info;
char filler[14];
} FIELD_REC;
Rob.
dbflist.c source:
/***
* dbflist.c
*
* Stand alone code to list the contents of a database file.
* DBF file name is passed on command line, include .dbf extension.
*
* 1 - Ensure parameter is passed.
*
* 2 - Open file.
*
* 3 - Read header.
*
* 4 - Build linked list of FIELD_RECs.
*
* 5 - Seek to start of data.
*
* 6 - For each record:
*
* 6.1 - Read it.
*
* 6.2 - Print deleted character (* or space)
*
* 6.3 - For each field:
*
* 6.3.1 - Print it.
*/
/* headers for functions we use */
#include "stdio.h"
#include "stdlib.h"
#include "fcntl.h"
#include "string.h"
#include "io.h"
#include "malloc.h"
#include "nandef.h"
#include "dbf.h"
typedef struct fl_tag
{
FIELD_REC field_rec;
struct fl_tag *next_flist;
} FIELD_LIST;
main(argc, argv)
int argc;
char *argv[];
{
FIELD_REC field_rec;
FIELD_LIST *field_list, *flist_ptr, *last_flist;
DBF_HEAD dbf_head;
int handle;
Boolean more_fields;
unsigned rec_num, rec_offset, width, num_fields;
char *record, *buffer;
if (argc != 2)
{
printf("Gebruik: dbflist <filenaam>\n"

;
exit(1);
}
handle = open(argv[1], O_RDONLY | O_RAW);
if (handle == -1)
{
printf("%s niet kunnen openen\n", argv[1]);
exit(1);
}
if (read(handle, (char *) &dbf_head, sizeof(dbf_head))
!= sizeof(DBF_HEAD))
{
printf("\nLeesfout\n"

;
exit(1);
}
/* build dynamic field_list linked list */
last_flist = NULL;
more_fields = TRUE;
num_fields = 0;
/* for each field */
while (more_fields)
{
more_fields = (read(handle, (char *) &field_rec,
sizeof(FIELD_REC)) == sizeof(FIELD_REC));
if (more_fields)
{
/* is this last field ? */
more_fields = (field_rec.field_name[0] != 0x0D);
if (more_fields)
{
/* no, add to list */
num_fields = num_fields + 1;
flist_ptr = (FIELD_LIST *) malloc(sizeof(FIELD_LIST));
memcpy((char *) &flist_ptr -> field_rec,
(char *) &field_rec, sizeof(FIELD_LIST));
flist_ptr -> next_flist = NULL;
/* is this the first entry in list ?? */
if (last_flist)
{
/* no, add to list */
last_flist -> next_flist = flist_ptr;
last_flist = flist_ptr;
}
else
{
/* yes, start list */
field_list = flist_ptr;
last_flist = field_list;
}
}
}
}
/* Now have field list built, list records */
flist_ptr = field_list;
/* position file to start of data */
if (lseek(handle, (long) dbf_head.data_offset, 0) == -1l)
{
printf("Fout bij zoeken in database file\n"

;
exit(1);
}
/* allocate space for 1 record */
record = (char *) malloc(dbf_head.rec_size);
/* This will cater for record with 1 field */
buffer = (char *) malloc(dbf_head.rec_size);
printf("\n\n"

;
/* for each record ... */
for (rec_num = 1; rec_num <= dbf_head.last_rec; rec_num++)
{
read(handle, record, dbf_head.rec_size);
printf("%4d ", rec_num);
/* for each field */
flist_ptr = field_list;
/* print deleted char */
printf("%c ", record[0]);
rec_offset = 1;
while (flist_ptr)
{
switch (flist_ptr -> field_rec.field_type)
{
case 'N':
width = flist_ptr ->
field_rec.len_info.num_size.len;
break;
default:
width = flist_ptr -> field_rec.len_info.char_len;
}
strncpy(buffer, (char *) &record[rec_offset], width);
buffer[width] = '\0';
printf("%s ", buffer);
rec_offset = rec_offset + width;
flist_ptr = flist_ptr -> next_flist;
}
printf("\n"

;
}
}
/* List the contents of a database file */