home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cslio205.zip / EXAMPLES / 2 / LOAD.CPP < prev    next >
Text File  |  1996-10-10  |  3KB  |  152 lines

  1. #include "iostream.h"
  2. #include "csmalloc.h"
  3. #include "demodb.h"
  4.  
  5.  
  6. ////////////////////////////////////////////////////////////////////////
  7. ///////////////  Display Intro screen //////////////////////////////////
  8. ////////////////////////////////////////////////////////////////////////
  9.  
  10. void help(void)
  11. {
  12.       cout<<endl<<endl;
  13.       cout<<"DEMO-2 version 1.0 for "<<_CP_PLATFORM<<endl;
  14.       cout<<"  Fills the database by parsing an input ASCII file."<<endl;
  15.       cout<<"Copyright (c) ComBits, 1996"<<endl;
  16.       cout<<"Compiled: "<<__DATE__<<",  "<<__TIME__<<endl<<endl;
  17.       cout<<"SYNTAX:  load <filename>     "<<endl;
  18.       cout<<"  filename: The name of the input file.  "<<endl<<endl;
  19.       cout<<"EXAMPLE: load college.txt     "<<endl;
  20. }
  21.  
  22. ////////////////////////////////////////////////////////////////////////
  23. ///////////////  Main program //////////////////////////////////////////
  24. ////////////////////////////////////////////////////////////////////////
  25.  
  26.  
  27.  
  28. int main(int argc,char *argv[])
  29. {
  30.  
  31.  
  32.  
  33. #define BUFFERLENGTH  200
  34.  
  35.  
  36.   char     LineBuffer[BUFFERLENGTH];
  37.   char     NameBuffer[BUFFERLENGTH];
  38.   char     DataBuffer[BUFFERLENGTH];
  39.  
  40.  
  41. //alloc_logging(TRUE);     // Uncomment if you like allocation logging.
  42.  
  43.   DEMO     demo;    // An instance of the generated DEMO class.
  44.  
  45.   if(argc!=2)
  46.   {
  47.     help();
  48.     return 1;
  49.   }
  50.  
  51.  
  52.  
  53. ////// Create Database and Indexes.
  54. ////// If they exist, they are OVERWRITTEN!
  55.  
  56.   if(!demo.define())
  57.   {
  58.      cout<<"Error, can't create database. "<<endl;
  59.      return 8;
  60.   }
  61.  
  62.  
  63.  
  64. ////// Open the now existing Database and Indexes.
  65.  
  66.   if(!demo.open(100))  // Open with 100Kb buffers.
  67.   {
  68.      cout<<"Error, can't open databases. "<<endl;
  69.      return 8;
  70.   }
  71.  
  72.  
  73.  
  74.  
  75. ////// Open input file.
  76.  
  77.  
  78.   FILE *fp;
  79.  
  80.  
  81.   fp=fopen(argv[1],"r");
  82.  
  83.   if(fp==NULL)
  84.   {
  85.  
  86.      cout<<"Error, can't open input file "<<argv[1]<<'.'<<endl;
  87.      return 12;
  88.  
  89.   }
  90.  
  91.  
  92.  
  93.   cout<<endl<<endl;
  94.  
  95. ////// Parse input file.
  96.  
  97.  
  98.   while(!feof(fp))
  99.   {
  100.  
  101.     if(fgets(LineBuffer,BUFFERLENGTH,fp))
  102.     {
  103.        trim_string(LineBuffer);   // Remove leading and trailing white space.
  104.        if(strlen(LineBuffer))      // Skipping empty lines.
  105.        {
  106.  
  107.       str_split(LineBuffer,' ',DataBuffer,NameBuffer);
  108.  
  109.       trim_string(NameBuffer);
  110.       trim_string(DataBuffer);
  111.  
  112.       demo.append();      // Insert empty record.
  113.  
  114.       demo.name(NameBuffer);  // Update name field.
  115.       demo.data(DataBuffer);  // Update data field.
  116.  
  117.       if(demo.numrec()%713==1) // Update counter every 713 records.
  118.          cout<<"\r Record "<<demo.numrec()<<'.';
  119.  
  120.        }
  121.     }
  122.  
  123.   }
  124.  
  125.  
  126.   cout<<"\r Record "<<demo.numrec()<<'.'<<endl;
  127.  
  128.  
  129. ////// Close input file.
  130.  
  131.   fclose(fp);
  132.  
  133.  
  134. ////// Close Database and Indexes.
  135.  
  136.   if(!demo.close())
  137.   {
  138.      cout<<"Error, while closeing databases. "<<endl;
  139.      return 8;
  140.   }
  141.  
  142. ////// Display errors IF ANY! Otherwise nothing is displayed.
  143.  
  144.   demo.display_error();
  145.  
  146.  
  147. ////// Return error level 0 to the command line.
  148.  
  149.   return 0;
  150.  
  151. }
  152.