Solved

C++ (DOS) and Dbase files

Posted on 2000-03-08
4
233 Views
Last Modified: 2012-05-04
Can anyone tell me how to access a Dbase III file using C++ for DOS? Any help will be appreciated.
0
Comment
Question by:vicki9991
  • 3
4 Comments
 
LVL 14

Expert Comment

by:AlexVirochovsky
ID: 2599234
Viki, i wrote C++ programme, that
creates, reads and saves records in
DBF files. But for Windows! For Dos
i must rewrite and test it. Add PTS
and i'll make it. And write you EMail.
0
 

Author Comment

by:vicki9991
ID: 2603335
thank you Alex. I am waiting...
0
 
LVL 14

Expert Comment

by:AlexVirochovsky
ID: 2603369
Viki, you's EMail??  
0
 
LVL 14

Accepted Solution

by:
AlexVirochovsky earned 100 total points
ID: 2609604
#include <dos.h>
#include <stdlib.h>
#include <conio.h>
#include <stdio.h>
#include <io.h>
#include <string.h>
#include <sys\stat.h>
#include <fcntl.h>
typedef enum                              // Field Val Check type
   {
      fldvNOCHECKS     = 0,                  // Does not have explicit val checks
      fldvHASCHECKS    = 1,                  // One or more val checks on the field
      fldvUNKNOWN      = 2                   // Dont know at this time
   } FLDVchk;
typedef enum                              // Field Rights
   {
      fldrREADWRITE     = 0,                 // Field can be Read/Written
      fldrREADONLY      = 1,                 // Field is Read only
      fldrNONE          = 2,                 // No Rights on this field
      fldrUNKNOWN       = 3                  // Dont know at this time
   } FLDRights;

typedef struct {                        // Field Descriptor
      int            iFldNum;                // Field number (1..n)
      char           szName[41];             // Field name
      unsigned int   iFldType;               // Field type
      unsigned int   iSubType;               // Field subtype (if applicable)
      unsigned int   iUnits1;                // Number of Chars, digits etc
      unsigned int   iUnits2;                // Decimal places etc.
      unsigned int   iOffset;                // Offset in the record (computed)
      unsigned int   iCol;                   // Column on the Screen
   } FLDDesc;


typedef struct DIRELEMENTtag{                          // Field Descriptor
      char szName[11];             // Field name
      char iFldType;               // Field type
      long lOffset;               // Offset in the record
      unsigned char iUnits1;         // Number of Chars, digits etc
      unsigned char iUnits2;       // Decimal places etc.
      char szReserv[14];         //reserve
   } DIRELEMENT;
#define fldUNKNOWN      0
#define fldZSTRING      1                 // Null terminated string
#define fldDATE         2                 // Date     (32 bit)
#define fldFLOAT        7                 // 64 bit floating point
int CreateTable(char *szTable,FLDDesc *fldDesc,int uNumFields);

void main()
{
  FLDDesc fldDesc[] =
      {
        { 1, "Field1", fldZSTRING, fldUNKNOWN,15,0, 0, 0},
        { 2, "Field2", fldZSTRING, fldUNKNOWN, 1,0, 0, 0},
        { 3, "Field3", fldZSTRING, fldUNKNOWN,20,0, 0, 0},
        { 4, "Field4", fldZSTRING, fldUNKNOWN,20,0, 0, 0},
        { 5, "Field5", fldFLOAT,   fldUNKNOWN, 4,0, 0, 0},
        { 6, "Field6", fldDATE,    fldUNKNOWN, 8,0, 0, 0}
      };

  CreateTable("test.dbf",fldDesc,6);
}
/*================================================================*/
/*  Name       : ReadFldDesc                                */
/*  Descript   : Read Description of Fields                    */
/*  Parameters : Name of File                                */
/*  Return     : Number of Field (0: No)                    */
/*================================================================*/
void ReadFldDesc(char *szTable,FLDDesc *fldDesc, int uNumFields)
{
  FILE *hFile = fopen (szTable, "rb");      //open Table
  if (hFile)            //OK
    {
      int i;
      lseek(hFile->fd, 32l, 0);                 //begin of Fields
      for (i = 0; i < uNumFields; i++)       //for all filds
      {
        DIRELEMENT dir;                        //element of directory
        read(hFile->fd, &dir, sizeof(DIRELEMENT) );      //read
        strcpy(fldDesc[i].szName,dir.szName);            //name
        fldDesc[i].iUnits1 = dir.iUnits1;      //len of field
        fldDesc[i].iUnits2 = dir.iUnits2;      //decimal point
        fldDesc[i].iFldNum = i+1;                  //decimal point

        switch (dir.iFldType)                        //find type
          {
            case 'C': fldDesc[i].iFldType = fldZSTRING; break;
            case 'D': fldDesc[i].iFldType = fldDATE;    break;
            case 'N': fldDesc[i].iFldType = fldFLOAT;   break;
          }
      }
      for (i = 0; i < uNumFields; i++)
      fldDesc[i].iOffset = i == 0 ? 1 :      //place in Record
             fldDesc[i-1].iOffset + fldDesc[i-1].iUnits1;
    }
  fclose (hFile);
}
/*================================================================*/
/*  Name       : FindNumFields                                */
/*  Descript   : Find Number of Field                            */
/*  Parameters : Name of File                                */
/*  Return     : Number of Field (0: No)                    */
/*================================================================*/
int FindNumFields(char *szTable)
{
  FILE *hFile = fopen (szTable, "r+b");      //File of Index
  int uNumFields = 0;
  if (hFile)                              //ok
    {
      int nBegin;
      lseek(hFile->fd, 8l, 0);           //place of Num
      read(hFile->fd, &nBegin, sizeof(int));   //read

      uNumFields = (nBegin - 1 - 32)/32;   //number of fields
      fclose(hFile);
    }
   return uNumFields;
}
/*=============================================================*/
/* Name      : CreateTable()                                   */
/* Desciprion: This function creates a table by name and table */
/* Parameters: Number of File                                */
/*  Return   : -1: Error, 0: OK                               */
/*=============================================================*/
int CreateTable(char *szTable,FLDDesc *fldDesc,int uNumFields)
{
  long lTest = 0,lNum;
  char c = 3;
  int  Year,nBegin,nLen,i;
  struct date dDate;

  int hFile = creat(szTable, S_IREAD |S_IWRITE);          //open Table
  if (hFile == -1)                //no file: Error!
    return -1;
  lseek(hFile, 0l, 0);                     //begin
  write(hFile, &c, sizeof(char));            //save 3: Db3!

  getdate(&dDate);
  Year = dDate.da_year%100;
  write(hFile, &Year, sizeof(char));          //save Year
  write(hFile, &dDate.da_mon, sizeof(char));  //same mon
  write(hFile, &dDate.da_day, sizeof(char));  //save day
  write(hFile, &lTest, sizeof(long));        //save 0
  nBegin = 32 + 32*uNumFields + 1;   //begin of Data
  write(hFile, &nBegin, sizeof(int));        //save
  nLen = 1;                                 //len of record
  for (i = 0;i < uNumFields; nLen += fldDesc[i++].iUnits1);
  write(hFile, &nLen, sizeof(int));              //save Record len
  c = 0;                              //reserve
  for (i = 0; i < 20;i++)
    write(hFile, &c, sizeof(char));              //save 0
  for (i = 0; i < uNumFields; i++)             //for all filds
    {
      DIRELEMENT dir;                        //element of directory
      memset(&dir, 0, sizeof(DIRELEMENT));      //clear
      strcpy(dir.szName,fldDesc[i].szName);      //name
      strupr(dir.szName);                  //to Up case
      switch (fldDesc[i].iFldType)            //find type
      {
        case fldZSTRING:    dir.iFldType = 'C'; break;
        case fldDATE:       dir.iFldType = 'D'; break;
        case fldFLOAT:      dir.iFldType = 'N'; break;
      }
      dir.iUnits1 = fldDesc[i].iUnits1;            //len of field
      dir.iUnits2 = fldDesc[i].iUnits2;            //decimal point
      write(hFile, &dir, sizeof(DIRELEMENT) );//save
    }
  c = 0x0d;                              //end of Dir Section
  write(hFile, &c, 1);       //save end of Dir Sectiopn of file

  lNum = 0;                  //number of records
  c = 0x1A;
  write(hFile, &c, 1);       //end of file
  lseek(hFile, 0x1d, 0);           //place of Date
  c = 0x1b;
  write(hFile, &c, 1);       //set 1b
  if (lNum)
    {
      lseek(hFile, 4l, 0);           //place of N rec.
      write(hFile, &lNum, sizeof(long));       //save N rec
    }
  close (hFile);
  return 0;
}
/*======================================================*/
/*  Name       : ReadRecord                          */
/*  Descript   : Get data from  file to Memory              */
/*  Parameters : name of file, buffer, index of record  */
/*  Return     : 0: ok, -1: error                  */
/*======================================================*/
int  ReadFile(char *szTable,char *buff,long iNumRec)
{
  int hFile = open (szTable,O_RDONLY | O_BINARY);      //open Table

  long l,lOff,lNum;
  int ind,ret = -1;

  lseek(hFile, 4l, 0);            //form lNum current

  read(hFile, &lNum, sizeof(long));
  if (lNum >= 0 && iNumRec < lNum)                  //no data
    {
      short int len, nYear, nBegin;                       //len of record
      read(hFile, &nBegin, sizeof(int));   //read
      read(hFile,&len, sizeof(int));   //read len of record
      lOff = (long)nBegin + iNumRec * (long)len;
      lseek(hFile, lOff, 0);
      read(hFile,buff,len);
      ret = 0;
    }
  close(hFile);
  return ret;
}

/*================================================================*/
/*  Name       : AddRecord                                */
/*  Descript   : Put data to file                             */
/*  Parameters : Name of File, record,                          */
/*             : Save = 0: New Record, 1: Change Old, 2: Delete   */
/*             :        3: UnDelete                          */
/*             : iNumRec:  index of Record(for Change/Delete)        */
/*  Return     : -1: Error                                  */
/*================================================================*/
int AddRecord (char *szTable,char *buff,int bSave, long iNumRec)
{
  int hFile = open (szTable,O_RDWR | O_BINARY);      //open Table
  int nRet = -1;
  long lNum,lOff;
  int len, nYear, nBegin;                       //len of record
  struct date dDate;
                           //lenght:last bait : 0x1A;
  lseek(hFile, 0x4l, 0);       //begin of # records
  read(hFile,&lNum, sizeof(long)); //read #
  read(hFile, &nBegin, sizeof(int));   //read
  read(hFile,&len, sizeof(int));   //read len of record
  lOff = (long)nBegin + (bSave == 0 ? lNum: iNumRec) * (long)len;
  lseek(hFile, lOff, 0);
  if (bSave == 0 || bSave == 1)
    {
      *buff = ' ';
      if (write(hFile, buff, len) == len )
      nRet = 0;
    }
  else if (bSave == 2 &&
       write(hFile, "*", sizeof(char)) == sizeof(char))//delete!
    nRet = 0;
  else if (bSave == 3 &&
       write(hFile, " ", sizeof(char)) == sizeof(char))//UnDelete!
    nRet = 0;

  if (bSave == 0) //not exists Record
    write(hFile, "\0x1A", 1);       //end of file

  if (bSave == 0)        //new N of Record
    {
      lNum++;
      lseek(hFile, 0x4l, 0);       //begin of # records
      write(hFile,&lNum, sizeof(long)); //write back
    }
                                   //change date
  getdate(&dDate);
  lseek(hFile, 1l, 0);           //place of Date
  nYear = dDate.da_year%100;
  write(hFile, &nYear, sizeof(char));   //save Year
  write(hFile, &dDate.da_mon, sizeof(char));  //same mon
  write(hFile, &dDate.da_day, sizeof(char));  //save day
  close(hFile);
  return nRet;
}
If you have some Q about text,ask!
Alex
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

815 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

8 Experts available now in Live!

Get 1:1 Help Now