Solved

.dbf file format

Posted on 2004-04-26
16
1,222 Views
Last Modified: 2012-08-14
I wan't to write a C/C++ application which will create,open,read,write a data file which can also be opened from applications like Dbase III, Foxpro etc i.e in a standard .dbf format. I wan't to know the format of such a data base file. Like the the header, text portions and how to access them.

Thanks in advance.
Avik.
0
Comment
Question by:Avik77
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
  • 3
  • +3
16 Comments
 
LVL 11

Assisted Solution

by:griessh
griessh earned 60 total points
ID: 10921352
Hi Avik77,

This place should have everything you need: http://www.e-bachmann.dk/docs/xbase.htm

======
Werner
0
 
LVL 2

Author Comment

by:Avik77
ID: 10921880
Hi griessh,
          Looks good! I am trying to implement those for a sample .dbf file. Will be back later.

Avik.
0
 
LVL 17

Expert Comment

by:mokule
ID: 10922255
Maybe You'll be interesed in CDBFAPI.DLL.

http://www.whitetown.com/cdbfapi/functions.php3
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 17

Expert Comment

by:mokule
ID: 10922381
0
 
LVL 9

Assisted Solution

by:ankuratvb
ankuratvb earned 140 total points
ID: 10924130
0
 
LVL 2

Author Comment

by:Avik77
ID: 10930751
Well now the problem cuts down to access a binary file and extract records from it. Well I have no idea how to do it. First of all I open the file in binary read...
fp=fopen("aaa.dbf","rb");
...
...
Now I have to get to the correct offset and take out the codes and also interpret them as valid records. Now if I read a character like c=fgetc(fp), will I be able to make anything from it. Well I am totally confused. I wan't some hints.

Avik.
0
 
LVL 11

Expert Comment

by:griessh
ID: 10931812
So what did you try already?
0
 
LVL 2

Author Comment

by:Avik77
ID: 10932716
I got the complete format of the dbf file and now I wan't to generate a dbf file from my own application. Or maybe extract data from a dbf file. But the extraction process is giving me a headache.

Avik.
0
 
LVL 17

Expert Comment

by:mokule
ID: 10933013

So why didn't You try links to libraries provided by me or ankuratvb.
0
 
LVL 2

Author Comment

by:Avik77
ID: 10936026
I tried, but the ftp sites containing the libraries were not available. Again I wan't the application to design in C (Turbo C++ v3.0) but the required libraries I did not get from any of those sites. BUT the catch is that I wan't to work with the binary files and how to proceed.

Avik.
0
 
LVL 9

Assisted Solution

by:ankuratvb
ankuratvb earned 140 total points
ID: 10938291
Do you want to write the code yourself or will a library do?

If you do want to write the code yourself,

Now that you have information regarding the structure of a dbf file,
use the fseek() function to move to a certain offset in the file, and fread() to read a segment of the file to memory. Both functions are included in <stdio.h> and documented in online help.

Otherwise,a library for TurboC exists here:
http://garbo.uwasa.fi/pc/c-lang.html

0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10938429
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 300 total points
ID: 11061122
I have translated (C++ to C, spanish to english) my own C++ DBF library for you.
This is a read-only library. I have not tried it in C version, could contain erros, please be patient. Hope to help you.
Here is the resume:

FILE *OpenDBF(char *filename)  -- Opens a DBF file and reads structure in global variables, returns NULL if error
CloseDBF(FILE *fp)   -- Closes DBF file and destroy structure in memory
struct TDBFField *LocateField(char *fieldname)  -- Returns a pointer to a field's information, NULL if error
int SearchReg(FILE *fp, char fieldname, char key)  -- Search an specific record, using a key
int GetFieldValue(FILE *fp, char *fieldname, char *buffer)  -- Get the content of the current record field
int NextReg()  -- Jumps to the next field

You can also go to an specific field simply changing CURREG value

------------------------------------------------
/* Pure C Language DBF File access mini-library */
/* By Jaime Olivares */
#include <stdio.h>

/* fixed-lenght buffer for this example */
#define MAX_FIELD_WIDTH 255

struct TDBFField {
      char fieldName[11];
      int fieldOffset;
      short fieldLong;
      struct TDBFField *next;
}

struct TDBFField *FIELDS = NULL;
static int CURREG  = -1;
static int REGSIZE = 0;
static int NUMREGS = 0;
static int HDRLONG = 0;


FILE *OpenDBF(char *filename)
{
      FILE *fp;

      if (fp=fopen(filename, "r")) {
            struct TDBFField *field;
            char  fieldName[11];
            short fieldLong;

            fseek(fp, 4, SEEK_SET);
            fread(&NUMREGS, 4);
            fread(&HDRLONG, 2);
            fread(&REGSIZE, 2);
            
            fieldName[10] = '\0';
            for (int numFields = (HDRLONG>>5)-1; numFields; numFields--) {
                  fseek(fp, 32*numFields, SEEK_SET);
                  fread(fieldName, 10);
                  fseek(6, SEEK_CUR);
                  fread(&fieldLong, 1);

                  field = (struct TDBFField *)malloc(sizeof(struct TDBFField));
                  strcpy(field->fieldName, fieldName);
                  field->fieldLong = fieldLong;
                  field->next = FIELDS;
                  FIELDS = field;
            }
            for (REGSIZE=1, field=FIELDS; field; field=field->next) {
                  field->fieldOffset = REGSIZE;
                  REGSIZE += field->fieldLong;
            }
      }
      return fp;
}

CloseDBF(FILE *fp)
{
      TDBFField *actual, *next;

      actual = FIELDS;
      while (actual) {
            next = actual->sgte;
            free (actual);
            actual = sgte;
      }
        FIELDS = NULL;

      fclose(fp);
}

struct TDBFField *LocateField(char *fieldname)
{
      TDBFField *field;

      for (field=FIELDS; field; field=field->sgte)
            if (!strcmp(field->fieldName, fieldname))  /* fieldname comparison is case sensitive */
                  return field;
      return NULL;
}

int SearchReg(FILE *fp, char fieldname, char key)
{
        /* simplified static buffer, for this example */
      char buffer[MAX_FIELD_WIDTH+1];

      if (!NUMREGS)
            return false;
      
      if (LocateField(fieldname)) {
            if (CURREG>=NUMREGS)
                  return false;

            do {
                  GetFieldValue(fp, fieldname, buffer);
                  if (!strncmp(buffer, key, strlen(key)))    /* key comparison is case sensitive */
                        return true;
            } while (NextReg());
      }
      return false;
}

int GetFieldValue(FILE *fp, char *fieldname, char *buffer)    /* take care about buffer length */
{
      struct TDBFField *field = LocateField(fieldname);

      if (field) {
            fseek(fp, HDRLONG+CURREG*REGSIZE+actual->fieldOffset, SEEK_SET);
            fread(fp, buffer, field->fieldLong);
            buffer[field->fieldLong] = '\0';
            return true;
      }
      return false;
}

int NextReg()
{
      if (++CURREG >= NUMREGS)
            return false;
      else
            return true;
}
0
 
LVL 2

Author Comment

by:Avik77
ID: 11061655
Thanks jaime_olivares,
       give me a little time to test and I will be responding soon. I think this was precisely what I needed.

Avik.

0
 
LVL 2

Author Comment

by:Avik77
ID: 11100045
Great work jaime_olivares! With a little modification I have been able to do what I wanted to do. Now I start splitting points . Thanks everybody for participating and giving ur valuable suggestions.

Avik.
0
 

Expert Comment

by:VisionC
ID: 11594737
Hi Avik77!

I have to work with dbf files too and i know you have good source code to do this, could you send it to me please?, i really need it. Thanks
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
In C, why can't I shift a 64-bit integer more than 32 bits? 13 267
how to understand recursion 12 232
C language IDE – Compilers installation 14 78
smtp c source code 7 105
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question