一、目的 硬件: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等),增加一些功能,使程序更加完美。 本文是几年前所作,希望对初学者有所帮助。 |