home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / database / db_frm_c.zip / DBCOPY.C < prev    next >
Text File  |  1987-06-19  |  2KB  |  96 lines

  1. /* 
  2. **        file:        dbcopy.c
  3. **        purpose:    program to copy structure and records to another file
  4. **        usage:    dbcopy fromfile tofile
  5. **        notes:    dos copy does this faster, but this program exercizes most
  6. **                    of the dbf functions
  7. **        author:    Mark Sadler
  8. **        revised:    6/19/87
  9. */
  10.  
  11.  
  12. #include <stdio.h>
  13. #include <alloc.h>
  14. #include <string.h>
  15. #include "dbf.h"
  16.  
  17. void db_error(int errornum,char *filename)
  18. {
  19.     printf("\nError opening file: ");
  20.     switch (errornum)
  21.     {
  22.         case OUT_OF_MEM:
  23.             printf("Not enough memory.\n");
  24.             break;
  25.         case NO_FILE:
  26.             printf("Can not open file %s.\n",filename);
  27.             break;
  28.         case BAD_FORMAT:
  29.             printf("File %s is not a dBASE III file.\n",filename);
  30.             break;
  31.     }
  32. }
  33.  
  34. void main(int argc,char *argv[])
  35. {
  36.     struct DBF in,out;
  37.     int errornum,fld;
  38.     unsigned long rec;
  39.     char *buff;
  40.  
  41.     if(argc < 2)
  42.         {
  43.         printf("Usage DBCOPY infile outfile\n");
  44.         exit(1);
  45.         }
  46.     
  47.     /* open input file */
  48.     strcpy(in.filename,argv[1]);
  49.     if(!strchr(in.filename,'.'))            /* default to .dbf file     */
  50.         strcat(in.filename,".DBF");
  51.     if((errornum = d_open(&in))!=0)        /* open file                 */
  52.     {
  53.         db_error(errornum,in.filename);
  54.         exit(1);
  55.     }
  56.  
  57.     /* copy input file structure to outputfile */
  58.     strcpy(out.filename,argv[2]);
  59.     if(!strchr(out.filename,'.'))                    /* default to .dbf file     */
  60.         strcat(out.filename,".DBF");
  61.     if((errornum = d_cpystr(&in,&out))!=0)        /* open file                 */
  62.     {
  63.         db_error(errornum,out.filename);
  64.         exit(1);
  65.     }
  66.  
  67.     for (rec=1L;rec <= in.records;rec++)            /* records loop */
  68.         {
  69.         d_getrec(&in,rec);
  70.  
  71.         /* 
  72.         **        note: this loop could be replaced by just copying the record with
  73.         **                using memcopy to copy to the out file record.  faster still
  74.         **                by changing the out file structure record pointer to point
  75.         **                to the record in the in file structure.
  76.         */ 
  77.         if ((buff=(char *)malloc(MAX_RECORD))==NULL)
  78.             {
  79.             db_error(OUT_OF_MEM,"filename error");
  80.             exit(1);
  81.             }
  82.         for (fld=1;fld <= in.num_fields;fld++)        /* fields loop  */
  83.             {
  84.             d_getfld(&in,fld,buff);
  85.             d_putfld(&out,fld,buff);
  86.             }    /* fields loop  */
  87.  
  88.         *out.record_ptr = *in.record_ptr;        /* copy deleted file marker */
  89.         d_addrec(&out);
  90.         }    /* records loop */
  91.     free(buff);
  92.     d_close(&in);
  93.     d_close(&out);
  94. }
  95.  
  96.