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

本文介绍如何使用C++读取.dbf文件格式。通过设计Field类和DBaseFile类,代码能读取任意.dbf文件的字段和记录信息。文章提供了具体的C++代码实现。

.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()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

geocat

球球大佬们赏赐点吃喝!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值