.dbf文件格式

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

  1. DBF文件格式
  2. shp系列(三)

关于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;class Field
{public:Field();void storeFieldContent();enum _eRecordItemDataType{B,C,D,G,L,M,N};  // 记录项的数据类型// 属性—— 1. 文件头中字段的内容:32字节// 0-10字节为记录项(字段)名称char _cTitle[11];// 11字节为记录项的数据类型char _cDataType;// 16字节为记录项长度,BYTE类型,1个字节// 注:可以用强制类型转换将记录项长度转换成int型unsigned char _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"using namespace std;class DBaseFile
{public:// 构造函数DBaseFile();DBaseFile(string sFilename);// 自定义函数// loadFile(string)函数将文件读取到内存void loadFile(string sFilename);void showData();// 数据表的增删改查void addRec(int iColum, int iLine, unsigned char* ucData);void deleteRec(int iColum, int iLine, unsigned char* ucData);void modifyRec(int iColum, int iLine, unsigned char* ucData);void checkRec(int iColum, int iLine, unsigned char* ucData);// 属性// 文件头中的内容int _iRecCount; // 记录的条数——行数int _iFieldCount;   // 字段数——列数short _BytesOfFileHead; // 文件头中的字节数short _BytesOfEachRecord;   // 每一条记录的字节数// 内存中用来存储相应数据的变量Field* _pField; // 用来创建某个字段string _sFilename;  // 用来存储文件名
//    vector<char*> _vFieldNameInFileHead;   // 文件头中的字段名vector<Field*> _vTable;  // 用来存储所有的字段的内容,是一个存储字段容器protected:void readFileHead(ifstream& inFile);void readFileRecord(ifstream& inFile);bool isReadFileOK(string sFilename);
};#endif // DBASEFILE_H

3. 具体实现

field.cpp

#include "field.h"Field::Field()
{}void Field:: storeFieldContent()
{_vField.push_back(_cFieldContent);
}

dbasefile.cpp

#include "dbasefile.h"
#include <iostream>DBaseFile::DBaseFile()
{}DBaseFile::DBaseFile(string sFilename)
{this->_sFilename=sFilename;
}void DBaseFile:: loadFile(string sFilename)
{ifstream inFile(sFilename,ios::binary|ios::in);if(!isReadFileOK(sFilename))return;readFileHead(inFile);readFileRecord(inFile);inFile.close();
}void DBaseFile:: showData()
{for(unsigned int i=0;i<_vTable.size();i++){cout<<_vTable[i]->_cTitle<<"\t";}cout<<"\n";for(int i=0;i<_iRecCount;i++){for(unsigned int j=0;j<_vTable.size();j++)cout<<_vTable[j]->_vField[i]<<"\t";cout<<endl;}
}bool DBaseFile::isReadFileOK(string sFilename)
{ifstream inFile(sFilename,ios::binary|ios::in);if(inFile.good()){inFile.close();return true;}elsereturn false;
}void DBaseFile:: readFileHead(ifstream& inFile)
{if(!isReadFileOK(_sFilename))return;// 读取文件头中的记录条数,即行数inFile.seekg(4,ios::beg);inFile.read((char*)&_iRecCount,sizeof (int));// 读取文件头的字节数inFile.read((char*)&_BytesOfFileHead,sizeof (short));// 读取一条记录中的字节长度inFile.read((char*)&_BytesOfEachRecord,sizeof (short));// 计算字段数,即列数_iFieldCount = (_BytesOfFileHead-33)/32;// 开始读取文件头中关于字段的描述for(int i=0;i<_iFieldCount;i++){inFile.seekg(32+32*i,ios::beg);_pField=new Field;for(int j=0;j<11;j++)   // 读取字段名,存入_cTitle数组inFile.read(_pField->_cTitle+j,sizeof (char));inFile.read(&_pField->_cDataType,sizeof (char));    // 读取字段的数据类型并存入_cDataTypeinFile.seekg(4,ios::cur);inFile.read((char*)&_pField->_ucFieldLength,sizeof (char));    // 读取字段长度,此时的字段长度为BYTE类型,需通过强制类型转换成int型_vTable.push_back(_pField);}
}void DBaseFile:: readFileRecord(ifstream& inFile)
{inFile.seekg(_BytesOfFileHead,ios::beg);char cDeleteTag;for(int i=0;i<_iRecCount;i++){inFile.read(&cDeleteTag,sizeof (char));for(unsigned int j=0;j<_vTable.size();j++){char*cRecord=new char[(int)_vTable[j]->_ucFieldLength];for(int k=0;k<(int)_vTable[j]->_ucFieldLength;k++)inFile.read(cRecord+k,sizeof (char));_vTable[j]->_vField.push_back(cRecord);}}
}void DBaseFile:: addRec(int iColum, int iLine, unsigned char* ucData)
{}void DBaseFile:: deleteRec(int iColum, int iLine, unsigned char* ucData)
{}void DBaseFile:: modifyRec(int iColum, int iLine, unsigned char* ucData)
{}void DBaseFile:: checkRec(int iColum, int iLine, unsigned char* ucData)
{}

总结

本文从DBF文件格式出发,抽象Field类和DBaseFile类,代码具有一定的通用性,对于一般情况下的DBF文件,本文代码都能读取。但是本文仅仅只是将DBF文件读到内存,尚未涉及文件的写操作,因此具有一定的局限性。但对于DBF文件格式,本文代码可以帮助读者理解。

关于如何使用C++读取.dbf文件相关推荐

  1. matlab 读取dbf文件

    前言 前几天需要对很多个ArcGIS输出的dbf文件做字段计算,但是!用模型构建器ArcGIS老是甭!崩得人心态都要都要崩了!后来师姐点醒了我!把它当作单纯的表格,在数组里算不就好了?! 一.直接用x ...

  2. java maven 读取 dbf 文件 生成 表结构 示例代码

    介绍 通过读取一个文件夹下所有dbf文件 然后生成sql 依赖 <dependency><groupId>com.github.albfernandez</groupId ...

  3. python读取dbf文件、dbf转xls、入库Postgres

    python读取dbf文件,转xls文件 文章目录 python读取dbf文件,转xls文件 依赖库安装 代码实现 读取dbf dbf转xls dbf转xls(批量) dbf入库Postgres 参考 ...

  4. JDBF读取DBF文件

    DBF文件的读取方法: public List<Dbf> readerDbf(String path){         List<Dbf> dbfs = new ArrayL ...

  5. shp系列(六)——利用C++进行Dbf文件的写(创建)

    上一篇介绍了shp文件的创建,接下来介绍dbf的创建. 推荐结合读取dbf的博客一起看! 推荐结合读取dbf的博客一起看! 推荐结合读取dbf的博客一起看! 1.Dbf头文件的创建 Dbf头文件的结构 ...

  6. 用html打开dbf文件怎么打开,dbf格式文件是什么 dbf文件用什么软件打开

    有小伙伴反映说,清理电脑时看到了dbf文件,不知道这是哪种类型的文件,也不知道有什么作用,不敢轻易删除.数据库文件的扩展名,是很多企业单位用于数据交换.那么dbf格式的文件如何打开呢? dbf格式文件 ...

  7. ruby读dbf文件

    最近要写rails程序,其中涉及读取DBF文件,于是GOOGLE一下,发现方法: 1,升级gem,命令gem install dbf 2,以下代码就是主要的核心代码 #测试写RUBY读DBF @dbf ...

  8. java dbf文件_Java读写dbf文件

    展开全部 Java读取DBF文件,需要加载开源jar包JavaDBF.jar,然后使用62616964757a686964616fe4b893e5b19e31333337613133里面的方法进行读写 ...

  9. php 生成dbf,php 读取vfp 8.0,9.0所产生的dbf文件

    VFP 9.0 PHP odbc 求大神指导... 由VFP8.0以上制造的DBF文件,自带的Tabel Type> autoinc field 或者Blob field,想通过PHP读取,不行 ...

最新文章

  1. Ubantu18.04安装Vivado
  2. RCF的优势--分布式应用
  3. python设置tk退出_退出tkinter gui
  4. Java虚拟机(JVM)-1-内存区域
  5. 中国 AI 开发者真实现状:人才依赖海外引进,本科 AI 教育盛行
  6. 模型堆叠(Stacking)和模型融合的原理与实现以及一个库heamy的介绍
  7. XP蓝屏代码集(转)
  8. 服务器带宽打开网页很慢,移动宽带打开网页慢?两招治本移动宽带打开网页速度慢的解决方法...
  9. android卡通头像,Q版卡通头像设计
  10. mysql怎么将成绩划分等级_数据库mysql中case如何给成绩划分等级?
  11. 卡方检验——离散型特征相关性分析
  12. Virtualbox 学习 — network setting
  13. FFmpeg 中的一些参数意义(tbr, tbn, tbc)
  14. word2003流程图变成图片_Word中流程图怎么画 手把手教你制作!
  15. 辣鸡python导入不了函数库嘤嘤嘤(问题)
  16. linux命令之----sort命令用于将文本文件内容加以排序
  17. 小话设计模式(十)外观模式
  18. 通过FPGA计算矩阵的伪逆pinv
  19. NK8.1-WY20-两种排序方法
  20. 考虑阶梯式碳交易机制与电制氢的综合能源系统热点优化

热门文章

  1. 【测绘程序设计试题集】 试题04 最短路径计算
  2. 机器学习中为什么需要验证集,验证集与测试集的区别是什么?
  3. jQueryt通过id选择器获取元素
  4. CTF-RSA1(已知p、q、dp、dq、c)
  5. 华为设备ACL配置命令
  6. 最详细的大数据学习路线图
  7. SQL 多表联合查询
  8. 图像分割之常用损失函数-Dice Loss
  9. OpenCV cvBoundingRect应用举例
  10. 认证 (authentication) 和授权 (authorization) 的区别.md