.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;
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()

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

被折叠的 条评论
为什么被折叠?



