[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1251
  • Last Modified:

.dbf file format

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
Avik Dasgupta
Asked:
Avik Dasgupta
  • 6
  • 3
  • 3
  • +3
4 Solutions
 
griesshCommented:
Hi Avik77,

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

======
Werner
0
 
Avik DasguptaAuthor Commented:
Hi griessh,
          Looks good! I am trying to implement those for a sample .dbf file. Will be back later.

Avik.
0
 
mokuleCommented:
Maybe You'll be interesed in CDBFAPI.DLL.

http://www.whitetown.com/cdbfapi/functions.php3
0
Will You Be GDPR Compliant by 5/28/2018?

GDPR? That's a regulation for the European Union. But, if you collect data from customers or employees within the EU, then you need to know about GDPR and make sure your organization is compliant by May 2018. Check out our preparation checklist to make sure you're on track today!

 
mokuleCommented:
0
 
ankuratvbCommented:
0
 
Avik DasguptaAuthor Commented:
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
 
griesshCommented:
So what did you try already?
0
 
Avik DasguptaAuthor Commented:
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
 
mokuleCommented:

So why didn't You try links to libraries provided by me or ankuratvb.
0
 
Avik DasguptaAuthor Commented:
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
 
ankuratvbCommented:
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
 
Jaime OlivaresSoftware ArchitectCommented:
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
 
Avik DasguptaAuthor Commented:
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
 
Avik DasguptaAuthor Commented:
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
 
VisionCCommented:
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

SMB Security Just Got a Layer Stronger

WatchGuard acquires Percipient Networks to extend protection to the DNS layer, further increasing the value of Total Security Suite.  Learn more about what this means for you and how you can improve your security with WatchGuard today!

  • 6
  • 3
  • 3
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now