|
|
发表于 2023-8-27 15:10:53
|
显示全部楼层
山西省太原市
#include <string>
#include <sstream>
#include <io.h>
#include <fcntl.h>
#include <process.h>
#include <sys\stat.h>
#include <iostream>
#include <cstring>
#define byte unsigned char
// 常量赋值
#define MaxBuff 200000
#define COLMAX 40
#define ROWMAX 7000
#define STRLEN 20
using namespace std;
// 缓冲区
char buff[MaxBuff];
// 缓冲区下标
char *buffPt,*endPt;
//日期
struct Date{
int year,month,day;
};
// DBF记录项
struct ColStruct
{
char name[STRLEN];
char mod;
int offset;
int len;
int preci;
int workID;
int MDX;
char rows[ROWMAX][STRLEN];
};
//DBF头记录及数据实体
struct DBFstruct
{
int version;
Date date;
int rowNum;
int headSegLen;
int colLen;
int undoPoc;
int possword;
int LagDriID;
int colNum;
int MDX;
ColStruct cols[COLMAX];
bool isDel[ROWMAX];
}nDbf;
int buffLen;
void readHeadSeg();
void readCol();
void readRowData();
/*
读取DBF文件函数,参数为DBF文件路径,
使用此函数读入DBF并初始化.
*/
int getDbfFile(char path[]){
int fid = 0;
_sopen_s(&fid, path ,O_RDONLY | O_BINARY,_SH_DENYNO,_S_IREAD | _S_IWRITE );
if(fid == 0) return 1;
buffLen = _read(fid,buff,MaxBuff);
buffPt = &buff[0] , endPt = &buff[buffLen];
readHeadSeg();
readCol();
readRowData();
return 0;
}
int getInt(){
int tmp = *((int *) buffPt );
buffPt += 4;
return tmp;
}
short getShort(){
short tmp = *((short *) buffPt );
buffPt += 2;
return tmp;
}
byte getByte (){ return *((byte *) buffPt ++ ); }
char getChar(){ return *((char *) buffPt ++ ); }
void dropByte(int len){ buffPt+=len; }
void getString(char *_Dst , int len){
memcpy(_Dst , buffPt , len * sizeof(char) );
buffPt += len;
_Dst[len] = 0;
}
void readHeadSeg(){
nDbf.version = getByte();
nDbf.date.year = getByte();
nDbf.date.month = getByte();
nDbf.date.day = getByte();
nDbf.rowNum = getInt();
nDbf.headSegLen = getShort();
nDbf.colLen = getShort();
dropByte(2);
nDbf.undoPoc = getByte();
nDbf.possword = getByte();
dropByte(12);
nDbf.MDX = getByte();
nDbf.LagDriID = getByte();
dropByte(2);
}
void readCol(){
int i = 0;
for( ;*buffPt != 0x0D; i ++ ){
ColStruct *nCol = &nDbf.cols[i];
getString(nCol->name,11);
nCol->mod = getChar();
nCol->offset = getInt();
nCol->len = getByte();
nCol->preci = getByte();
dropByte(2);
nCol->workID = getByte();
dropByte(10);
nCol->MDX = getByte();
}
nDbf.colNum = i;
dropByte(1);
}
void readRowData(){
for( int i = 0 ;i < nDbf.rowNum ; i++ ){
if(getByte() == 0x2D )
nDbf.isDel[i] = 0;
else
nDbf.isDel[i] = 1;
for( int j = 0 ; j < nDbf.colNum ; j++)
getString( nDbf.cols[j].rows[i] , nDbf.cols[j].len );
}
}
/*
获得对应数据实体信息
参数为: < 数据实体号(也就是行号),记录项名(也就是列名),出口指针 >
_Dst为数组指针,读取到的信息将会写入_Dst数组内
*/
void getValue(int row, char colName[], void *_Dst){
if(row < nDbf.rowNum)
for(int i = 0 ;i < nDbf.colNum ; i++ ){
if( !strcmp(nDbf.cols[i].name , colName) ){
strcpy_s((char *)_Dst, STRLEN ,nDbf.cols[i].rows[row]);
return;
}
}
strcpy_s((char *)_Dst, STRLEN ,"NULL");
}
/*
重载函数——获得对应数据实体信息
参数为: < 数据实体号(行名),记录项号(列号),出口指针 >
_Dst为数组指针,读取到的信息将会写入_Dst数组内
*/
void getValue(int row , int col, void *_Dst){
if( col < nDbf.colNum && row < nDbf.rowNum )
strcpy_s((char *)_Dst, STRLEN, nDbf.cols[col].rows[row]);
else
strcpy_s((char *)_Dst, STRLEN ,"NULL");
}
//获得数据实体总数(行数)
int getColNum() { return nDbf.colNum; }
//获得数据项总数(列数)
int getRowNum() { return nDbf.rowNum; }
找到个 DBF格式的C语言版本读取源代码 |
|