CPP读取dbf文件

prop系统导出的交收数据为dbf文件格式,linux需要能够解析该格式文件,

找到一个开源库shapelib可以实现该功能;

1、下载库源码

http://download.osgeo.org/shapelib/

选择shapelib-1.4.0.zip这个版本;

2、解压下载的源码,编译,

[[email protected] dbf_demo]$ ./configure --prefix=/home/fm/dbf_demo/shapelib

此时会在源码目录生成MakeFile文件,注意修改以下地方

目的是不让编译contrib目录下面的例子代码,编译会报错;

执行 make & make install

源码编译安装完成;

3、编写测试例子代码:

[[email protected] dbf_demo]$ g++ -o test test.cpp -I ./shapelib/include/ -L ./shapelib/lib/ -lshp

test.cpp代码如下:

#include <stdlib.h>
#include <string.h>
#include "shapefil.h"

int main(
int argc,
char
** argv )

{
DBFHandle????hDBF;
int????????*panWidth, i, iRecord;
char????szFormat[32], szField[1024];
char????ftype[32], cTitle[32], nTitle[32];
int????????nWidth, nDecimals;
int????????cnWidth, cnDecimals;
DBFHandle????cDBF;
DBFFieldType????hType,cType;
int????????ci, ciRecord;

/* -------------------------------------------------------------------- */
/* Display a usage message. */
/* -------------------------------------------------------------------- */
if( argc !=
2
)
{
????printf(
"dbfinfo xbase_file\n"
);
????exit(
1
);
}

/* -------------------------------------------------------------------- */
/* Open the file. */
/* -------------------------------------------------------------------- */
hDBF = DBFOpen( argv[1],
"rb"
);
if( hDBF ==
NULL
)
{
????printf(
"DBFOpen(%s,\"r\") failed.\n", argv[1]
);
????exit(
2
);
}

printf ("Info for %s\n",argv[1]);

/* -------------------------------------------------------------------- */
/*????If there is no data in this file let the user know.????????*/
/* -------------------------------------------------------------------- */
i = DBFGetFieldCount(hDBF);
printf ("%d Columns, %d Records in file\n",i,DBFGetRecordCount(hDBF));

/* -------------------------------------------------------------------- */
/*????Compute offsets to use when printing each of the field ????????*/
/*????values. We make each field as wide as the field title+1, or ????*/
/*????the field value + 1. ????????????????????????*/
/* -------------------------------------------------------------------- */
panWidth =
(int
*) malloc( DBFGetFieldCount( hDBF )
*
sizeof(int)
);
????
????//按行读取
????for( i =
0; i < DBFGetRecordCount(hDBF); i++
)
{
????????for(
int j =
0; j < DBFGetFieldCount(hDBF); j++
)
????????{
????????????const
char
* pszVal = DBFReadStringAttribute( hDBF, i, j);
????????????printf ("%s\n",pszVal);
????????}
}
DBFClose( hDBF );
return(
0
);
}

原文地址:https://www.cnblogs.com/skiing886/p/9796959.html

时间: 2024-08-02 00:32:16

CPP读取dbf文件的相关文章

读取DBF文件的部分代码

private void BtnOpenInitial_Click(object sender, EventArgs e) { OpenFileDialog file = new OpenFileDialog(); if (file.ShowDialog() == System.Windows.Forms.DialogResult.OK) { string path = file.FileName; DTable dbf = new DTable(); dbf.Load(path); if (d

java读取dbf文件

1.下载DBFReader jar包 2.实例代码 package service; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.sql.Connection; import java.sql

读取DBF文件数据

#region 返回DBF表 public static System.Data.DataTable getDTFromDBF(string fullPath) { string pDir = System.IO.Path.GetDirectoryName(fullPath); string pFile = System.IO.Path.GetFileNameWithoutExtension(fullPath); return getDTFromDBF(pDir, pFile); } publi

新手上路之DBF文件的读取

初次了解DBF文件,因为有需求要将其中的数据导出,并插入到数据库中.开始的时候用Excel把它打开了,以为它就是一个Excel文件,就想着用NPOI来实现,显然是作为一个新人太天真了,后来在别人的博客上了解到,读取这个文件有多种方式,根据不同的难易用不同的方法,由于我所接触的这个文件中没有过多的约束,我也就用了最简单的一种. /// <summary> /// 读取DBF文件,此方法适用于简单的DBF文件,即类似深交所的文件 /// </summary> /// <param

dbf文件的解析方法

// 先下载导入dbf的jar架包  http://pan.baidu.com/s/1jIqVIcA InputStream fis = null; String sresult = ""; try { // 读取文件的输入流 fis = new FileInputStream(path); // 根据输入流初始化一个DBFReader实例,用来读取DBF文件信息 DBFReader reader = new DBFReader(fis); // 调用DBFReader对实例方法得到p

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

上一篇介绍了shp文件的创建,接下来介绍dbf的创建. 推荐结合读取dbf的博客一起看! 推荐结合读取dbf的博客一起看! 推荐结合读取dbf的博客一起看! 1.Dbf头文件的创建 Dbf头文件的结构如下: 记录项数组说明: 字段类型说明: 关于每项的具体含义参照读取dbf文件的解释,这里重点解释几项: HeaderByteNum指dbf头文件的字节数,数值不用除于2,具体为:从version到Reserved2(共32) + n个字段 * 每一个字段长度 32 + terminator. Re

net core封装DBF文件处理

前言 在.net core项目中需要读写dbf文件,可以使用FastDBF操作dbf文件,十分方便,使用Nuget可以添加FastDBF 代码实现 1 public class DBFUtil 2 { 3 private static List<KeyValuePair<string, PropertyInfo>> Mapping<T>() where T : class 4 { 5 Type type = typeof(T); 6 var properties = t

Java读取Level-1行情dbf文件极致优化(3)

最近架构一个项目,实现行情的接入和分发,需要达到极致的低时延特性,这对于证券系统是非常重要的.接入的行情源是可以配置,既可以是Level-1,也可以是Level-2或其他第三方的源.虽然Level-1行情没有Level-2快,但是作为系统支持的行情源,我们还是需要优化它,使得从文件读取,到用户通过socket收到行情,端到端的时延尽可能的低.本文主要介绍对level-1行情dbf文件读取的极致优化方案.相信对其他的dbf文件读取应该也有借鉴意义. Level-1行情是由行情小站,定时每隔几秒把d

Java读取Level-1行情dbf文件极致优化(2)

最近架构一个项目,实现行情的接入和分发,需要达到极致的低时延特性,这对于证券系统是非常重要的.接入的行情源是可以配置,既可以是Level-1,也可以是Level-2或其他第三方的源.虽然Level-1行情没有Level-2快,但是作为系统支持的行情源,我们还是需要优化它,使得从文件读取,到用户通过socket收到行情,端到端的时延尽可能的低.本文主要介绍对level-1行情dbf文件读取的极致优化方案.相信对其他的dbf文件读取应该也有借鉴意义. Level-1行情是由行情小站,定时每隔几秒把d