Category : Dbase (Clipper, FoxBase, etc) Languages Source Code
Archive   : D3.ZIP
Filename : D3.C

 
Output of file : D3.C contained in archive : D3.ZIP

/*****************************************************************************
* Functions to read dBASE III+ files *
* *
* dopen - open database, read header, fields, set up 'area' struct. *
* dclose - close open database file *
* dprthdr - print database header and data dictionary *
* dgetrec - read a record from the open database *
* dputrec - write a record to the open database *
* dprtrec - print a single record gotten by the dgetrec call *
* dgetfld - retreive a database record field *
* dputfld - put user data into field *
* dprtfld - print a single field *
* dskip - skip to next sequential record in data file *
* *
*****************************************************************************/

#define DB3MAIN /* forces include of function definitions, */
/* so it must be before #include "db3.h" */

#include
#include
#include
#include
#include
#include
#include "db3.h" /* stdio.h included in db3.h */

main(argc,argv)
int argc;
char *argv[];
{
DBAREA *area;
DBFIELD *field;
char *ext, infile[15];
unsigned long recno;
int retcode;

setbuf(stdout, NULL);
setbuf(stderr, NULL);

/* check argument count */
if (argc != 2) {
printf("usage: %s file[.dbf]\n", argv[0]);
exit(1);
}

ext = &argv[1][strlen(argv[1])-4]; /* point to ".dbf" */

if (strncmp(ext,".dbf", 4) != 0) /* no ".dbf" */
sprintf(infile, "%s.dbf", argv[1]);
else
strcpy(infile,argv[1]);

/* open database file */
area = dopen(infile, O_RDWR|O_BINARY);

/* open failed completely */
if (area == NULL) {
printf("DOPEN FAILED: %s\n", infile);
exit(1);
}

/* check for other error condition */
if (ERROR) {
printf("DOPEN ERROR: %d\n", ERROR);
exit(1);
}

printf ("\nPrint Database Header\n"); /* test function */
dprthdr(area, DB3_HEAD);

printf("RECCOUNT %ld\n", RECCOUNT);
printf("RECSIZE %d\n", RECSIZE);
printf("RECNO %ld\n", RECNO);
printf("LUPDATE %s\n", LUPDATE);
printf("VERSION %d\n", VERSION);
printf("DFB %s\n", DBF);
printf("ERROR %d\n", ERROR);
printf("FIELDS %d\n", FIELDS);

/* pause */
while (!kbhit());
getch();

printf ("\nPrint Data Dictionary\n"); /* test function */
dprthdr(area, DB3_DATA);

/* pause */
while (!kbhit());
getch();

recno = 1L;
while (recno <= 10L) {
if (ERROR) {
printf("ERROR: %d\n", ERROR);
break;
}
if (dgetrec(area, recno)) { /* test function */
printf ("\nDGETREC ERROR: %d\n", ERROR);
continue;
}
else
printf("RECNO = %ld\n", RECNO);

if (field = dgetfld(area, "number")) /* test function */
dprtfld(field); /* test function */

++recno;
}

printf ("\nRead Database Record #8\n");
if (!dgetrec(area, 8L)) { /* test function */
printf ("\nDGETREC SUCCEEDED\n");
dprtrec(area); /* test function */
}
else
printf ("\nDGETREC ERROR: %d\n",ERROR);

printf ("\nFind Field 'NUMBER'\n");
if (field = dgetfld(area, "number")) {
dprtfld(field);
if (!dputfld(area, "number", "000.0"))
printf("\nDPUTFLD SUCCEEDED\n");
else
printf("\nDPUTFLD ERROR: %d\n", ERROR);
}
else
printf ("\nField 'number' not found!\n");

printf ("\nWrite Database Record #8\n");

if (!dputrec(area, 8L))
printf ("\nDPUTREC SUCCEEDED\n");
else
printf ("\nDPUTREC ERROR: %d\n, ERROR");

/* pause */
while (!kbhit());
getch();

printf("Before skip RECNO = %ld\n", RECNO);

if ((retcode = dskip(area, 1)))
printf("DSKIP ERROR: %d retcode=%d\n", ERROR, retcode);

printf("After skip RECNO = %ld\n", RECNO);
dprtrec(area); /* test function */

printf("APPEND one BLANK RECORD\n");

/* pause */
while (!kbhit());
getch();

printf("Before APPEND RECCOUNT %ld\n", RECCOUNT);
retcode = dappend(area);
if (retcode)
printf("DAPPEND ERROR: %d retcode=%d\n",ERROR, retcode);

printf("After APPEND RECCOUNT %ld\n", RECCOUNT);
printf ("\nEND DEMO PROGRAM\n");

dclose(area);
exit(0);
}