Win32下Foxbase+數據庫瀏覽程序的編寫
2024-07-21 02:08:24
供稿:網友
一、目的
硬件:cpu (cyrix 200mhz ), 內存16m,硬盤4g
要求:在windows98的資源管理器中鼠標雙擊任何一個foxbase+數據庫文件圖標(每個文件數據記錄在一萬條以下),程序打開數據庫文件并顯示數據庫內容。
二、步驟
foxbase+數據庫文件格式(參照mark sadler的文件格式說明和相應c語言源程序文件)
編程中發生的問題:
以下是mark sadler的dbf.h文件部分內容:
typedef unsigned char uchar;
struct field_record /* this structure is filled in memory */
{ /* with a fread. do not change. */
char name[11]; /* name of field in asciz */
char typ; /* type of field...char,numeric etc. */
char *field_data_address; /* offset of field in record */
#if defined(__tiny__) || defined(__small__) || defined (__medium__)
int space_holder; /* field_data_address must be 32 bits */
#endif
uchar len; /* length of field */
uchar dec; /* decimals in field */
uchar reserved_bytes[14]; /* reserved by dbase */
};
struct dbf
{
char filename[maxpath]; /* dos filename */
file *file_ptr; /* c file pointer */
unsigned long int current_record;/* current record in memory */
enum /* status of file */
{
not_open=0,
not_updated,
updated
} status;
uchar num_fields; /* number of fields */
/* the following 7 variables are filled with a fread, do not change order or size */
uchar dbf_version; /* version character */
uchar update_yr; /* date of last update - year (-1900) */
uchar update_mo; /* date of last update - month */
uchar update_day; /* date of last update - day */
unsigned long int records; /* number of records in dbf */
unsigned int header_length; /* length of header structure */
unsigned int record_length; /* length of a record */
/* */
struct field_record *fields_ptr; /* pointer to field array */
char *record_ptr; /* pointer to current record struct */
};
int d_addrec(struct dbf *d);
int d_blank(struct dbf *d);
int d_close(struct dbf *d);
int d_cpystr(struct dbf *s,struct dbf *d);
char d_getfld(struct dbf *d,int f,char *buff);
int d_getrec(struct dbf *d,unsigned long int r);
int d_open(struct dbf *d);
int d_putfld(struct dbf *d,int f,char *buff);
int d_putrec(struct dbf *d,unsigned long int r);
以上資料中提供的函數都提供了源程序,雖然大部分都是使用ansi c,但卻是針對dos方式下的。如更新日期:
inregs.h.ah=0x2a;
intdos(&inregs,&outregs);
d->update_day=outregs.h.dl;
d->update_mo=outregs.h.dh;
d->update_yr=outregs.x.cx-1900;
這顯然在win32下無法編譯通過。但在dos下完全可以編譯通過,并準確地讀出各條記錄信息。
修改以上各函數,使之符合win32特點,編譯通過。
運行程序,發現無法正確顯示數據庫內容。
由于函數實現部分已經全部修改為win32可以接受的形式,沒什么問題,只有檢查dbf.h。
dos的int與char一樣為8bit, 而win32中,int 為32bit,smallint 為8bit。
修改dbf.h文件,將struct中所有的int 改為smallint,long int 改為int。
編譯通過,程序能正常運行。
三、總結
在win32沿用dos方式下的c程序時,要特別注意不同平臺下的區別。
以上程序還可適當加強,如:讀入各項數據時應新建一線程,并增加一進度條顯示數據庫文件讀入情況,再編寫一些函數(如find、delete等),增加一些功能,使程序更加完美。
本文是幾年前所作,希望對初學者有所幫助。