.dbf文件格式

.dbf文件格式描述可以看这两篇博客:

关于dbf文件格式笔者不再赘述,因为上述两篇博客已经讲的很明白了。这篇文章主要是要讲怎么通过C++来读取任意.dbf文件。

C++代码

1.Field类

.dbf是表文件,以二进制方式存储,头文件是变长的。
既然是表文件,那么就存在行列的概念。DBF表的行表示为记录,列表示为字段(field)。因此,可以设计一个字段类,即Class Field。
代码如下:

/********************************************************************************
 * Description: this header file is designed for reading and saving 
 *              the field in the dBaseFile
 * 
 * Author: Mr.Zhang Wanglin(Geocat)
 * 
 * Date: 2020.06.07
********************************************************************************/#ifndef FIELD_H#define FIELD_H#include<vector>using std::vector;classField{
   
   public:Field();voidstoreFieldContent();enum _eRecordItemDataType{
   
   B,C,D,G,L,M,N};// 记录项的数据类型// 属性—— 1. 文件头中字段的内容:32字节// 0-10字节为记录项(字段)名称char _cTitle[11];// 11字节为记录项的数据类型char _cDataType;// 16字节为记录项长度,BYTE类型,1个字节// 注:可以用强制类型转换将记录项长度转换成int型unsignedchar _ucFieldLength;// 字段内容char _cFieldContent[100];
    vector<char*> _vField;// 存储字段的内容};#endif// FIELD_H

2.DBaseFile类

DBaseFIle类包含文件头里的内容,以及所有字段的内容。
头文件代码如下:

/********************************************************************************
 * Description: this header file is designed for reading and saving
 *              the field in the dBaseFile
 *
 * Author: Mr.Zhang Wanglin(Geocat)
 *
 * Date: 2020.06.08
********************************************************************************/#ifndef DBASEFILE_H#define DBASEFILE_H#include<string>#include<vector>#include<fstream>#include"field.h"usingnamespace std;classDBaseFile{
   
   public:// 构造函数DBaseFile();DBaseFile(string sFilename);// 自定义函数// loadFile(string)函数将文件读取到内存voidloadFile(string sFilename);voidshowData()