• CPP读取dbf文件


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

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

    1、下载库源码

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

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

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

    [fm@vm178 dbf_demo]$ ./configure --prefix=/home/fm/dbf_demo/shapelib

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

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

    执行 make & make install

    源码编译安装完成;

    3、编写测试例子代码:

    [fm@vm178 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 " );
        exit( 1 );
    }

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

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

    /* -------------------------------------------------------------------- */
    /*    If there is no data in this file let the user know.        */
    /* -------------------------------------------------------------------- */
    i = DBFGetFieldCount(hDBF);
    printf ("%d Columns, %d Records in file ",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 ",pszVal);
            }
    }
    DBFClose( hDBF );
    return( 0 );
    }

  • 相关阅读:
    union和union all 合并查询
    c#中取整,向上取,向下取
    多个DataSet数据合并
    js未定义判断
    C# 获取时间差状态
    SQL中inner join、outer join和cross join的区别
    朗朗跄跄的受伤,跌跌撞撞的坚强
    IIS7.5 平台.NET无后缀名伪静态实现办法-服务器配置
    检查Android系统版本
    js 实现搜索功能
  • 原文地址:https://www.cnblogs.com/skiing886/p/9796959.html
Copyright © 2020-2023  润新知