Solved

.dbf file format

Posted on 2004-04-26
16
1,209 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
  • 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
 
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
How to setup iphone app subscription service 3 99
FILE closing 11 68
Which checksum is this? 7 136
Passing a array as parameter - C 2 77
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now