发信人: xiaomiao()
整理人: yangcs(2000-06-25 15:52:07), 站内信件
|
下面是我写的程序,请大家指教!
/***************************************************************
Program to convert dbf format file to
tab-delimited text file
Written by XiaoMiao 2000.06
dbf file structure
~~~~~~~~~~~~~~~~~~
bytes description
00 foxbase+, foxpro, dbaseiii+, dbaseiv, no memo - 0x03
foxbase+, dbaseiii+ with memo - 0x83
foxpro with memo - 0xf5
dbaseiv with memo - 0x8b
dbaseiv with sql table - 0x8e
01-03 last update, format yyyymmdd **correction: it is yymmdd**
04-07 number of records in file (32-bit number)
08-09 number of bytes in header (16-bit number)
10-11 number of bytes in record (16-bit number)
12-13 reserved, fill with 0x00
14 dbaseiv flag, incomplete transaction
begin transaction sets it to 0x01
end transaction or rollback reset it to 0x00
15 encryption flag, encrypted 0x01 else 0x00
changing the flag does not encrypt or decrypt the records
16-27 dbaseiv multi-user environment use
28 production index exists - 0x01 else 0x00
29 dbaseiv language driver id
30-31 reserved fill with 0x00
32-n field descriptor array
n+1 header record terminator - 0x0d
field descriptor array table
bytes description
0-10 field name ascii padded with 0x00
11 field type identifier (see table)
12-15 displacement of field in record
16 field length in bytes
17 field decimal places
18-19 reserved
20 dbaseiv work area id
21-30 reserved
31 field is part of production index - 0x01 else 0x00
field identifier table
ascii description
c character
d date, format yyyymmdd
f floating point
g general - foxpro addition
l logical, t:t,f:f,y:y,n:n,?-not initialized
m memo (stored as 10 digits representing the dbt block number)
n numeric
p picture - foxpro addition
note all dbf field records begin with a deleted flag field.
if record is deleted - 0x2a (asterisk) else 0x20 (space)
end of file is marked with 0x1a
****************************************************************/
#define DEBUG 1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* DBF文件头结构 */
struct dbf_head {
char vers; /* version identifier */
unsigned char yy,mm,dd; /* yymmdd */
unsigned long int no_recs; /* records number */
unsigned int head_len,rec_len; /* head_length,record_length */
char reserved[20]; /* reserved bytes */
};
/* 字段描述结构 */
struct field_element{
char field_name[11];
char field_type;
unsigned long int offset;
unsigned char field_length;
unsigned char field_decimal;
char reserved1[2];
char dbaseiv_id;
char reserved2[10];
char production_index;
};
int main(int argc,char **argv){
FILE *fp_dbf,*fp_out;
struct dbf_head file_head;
struct field_element *fields;
char *buffer;
char *output;
char *ptr;
int i;
int fields_count;
char field_type;
long counts;
if(argc!=3){
fprintf(stderr,"Usage: %s <input_dbf_filename> <output_text_filename> \n",argv[0]);
exit(1);
}
if((fp_dbf=fopen(argv[1],"rb"))==NULL){
fprintf(stderr,"Cannot open file %s to read!\n",argv[1]);
exit(2);
}
if((fp_out=fopen(argv[2],"wt"))==NULL){
fprintf(stderr,"Cannot open file %s to write!\n",argv[2]);
exit(3);
}
fseek(fp_dbf,0L,SEEK_SET);
fread((void*)&file_head,sizeof(struct dbf_head),1,fp_dbf);
fields_count=(file_head.head_len-sizeof(struct dbf_head)-1)/sizeof( struct field_element);
if((fields=(struct field_element*)malloc(sizeof(struct field_elemen t)*fields_count))==NULL){
fprintf(stderr,"Cannot allocate memory for fields array !\n");
fclose(fp_dbf);
fclose(fp_out);
exit(4);
}
if((buffer=(char*)malloc(sizeof(char)*file_head.rec_len))==NULL){
fprintf(stderr,"Cannot allocate memory for record buffer !\n");
fclose(fp_dbf);
fclose(fp_out);
exit(5);
}
if((output=(char*)malloc(sizeof(char)*file_head.rec_len))==NULL){
fprintf(stderr,"Cannot allocate memory for output buffer !\n");
fclose(fp_dbf);
fclose(fp_out);
exit(6);
}
fread((void*)fields,sizeof(struct field_element),fields_count,fp_db f);
fprintf(stdout,"Start converting dbf file to text file,please wait. ..\n");
/* print the header */
for(i=0;i<fields_count;i++){
if(i==fields_count-1)
fprintf(fp_out,"%s\n",fields[i].field_name);
else
fprintf(fp_out,"%s\t",fields[i].field_name);
}
fseek(fp_dbf,(long)file_head.head_len,SEEK_SET);
/* print the records */
for(counts=0;counts<file_head.no_recs;counts++){
if(fgetc(fp_dbf)==(int)'\x2a') /* if it is a deleted flag */
continue;
fread((void*)buffer,file_head.rec_len-1,1,fp_dbf);
buffer[file_head.rec_len]='\0';
ptr=buffer;
for(i=0;i<fields_count;i++){
strncpy(output,ptr,fields[i].field_length);
ptr+=fields[i].field_length;
output[fields[i].field_length]='\0';
if(i<fields_count-1)
fprintf(fp_out,"%s\t",output);
else
fprintf(fp_out,"%s\n",output);
}
}
fprintf(stdout,"Convertine is over!\n");
fclose(fp_dbf);
fclose(fp_out);
free(buffer);
free(output);
return 0;
}
-- /******************************************
低调,唯美,内省,黑色,简约,折衷,颓废,梦呓,
糜烂,迷乱,阴郁,婉约,低吟,根源,氛围,元素,
极端,低迷,扭曲,爆裂,失落,充斥,具象,聆听,
压抑,气息,炼狱,冰冷,理念,郁闷,神伤,实验,
回归,迷幻,迷离,内敛,艰涩,严肃,模糊,前卫。
※ 来源:.月光软件站 http://www.moon-soft.com.[FROM: 202.103.85.54]
|
|