Entering Book Information (Cont.)


The C++ code below, which is initiated by the Enter books button of the Interface Enter Book Information, performs the following tasks:
  1. Saves the four pieces of book information submitted from the Web.
  2. Saves them to a Book object. No duplication is checked.
  3. Writes the information to the file named books.txt which contains fixed-length records where a record is a book and takes one row. Each record consists of four fixed-length fields:
    • a title of 50 bytes,
    • an ISBN of 15 bytes,
    • a price of 7 bytes, and
    • a quantity of 5 bytes.
    Data items are separated by the symbol ‘|’.
Note that
 ~wenchen/public_html/cgi-bin/351/week5/EnterBooks1.cpp 
#include  <fstream>
#include  <iostream>
#include  <cstdlib>
#include  <cstring>
  using namespace  std;
class  Book {
  public:
    char  title[51];
    char  ISBN[16];
    char  price[8];
    char  quantity[6];
    void  ReadData( char*, char*, char*, char* );
};
void  Book::ReadData( char* a, char* b, char* c, char* d ) {
  fstream  file;
  char     spaces[51] = "                                                  ";

  strcpy ( title, spaces );
  strncpy( title, a, strlen(a) );
  strcpy ( ISBN, spaces );
  strncpy( ISBN, b, strlen(b) );
  strcpy ( price, spaces );
  strncpy( price, c, strlen(c) );
  strcpy ( quantity, spaces );
  strncpy( quantity, d, strlen(d) );

  file.open ( "books.txt", fstream::out|fstream::app );
  file.write( title, 50 );
  file << "|";
  file.write( ISBN, 15 );
  file << "|";
  file.write( price, 7 );
  file << "|";
  file.write( quantity, 5 );
  file << "|";
  file << endl;
  file.close( );
};
int main( int argc, char* argv[ ] ) {
  char  title[51], ISBN[16], price[8], quantity[6];
  Book  b;
 
  strcpy( title,    argv[1] ); 
  strcpy( ISBN,     argv[2] );
  strcpy( price,    argv[3] ); 
  strcpy( quantity, argv[4] );
  b.ReadData( title, ISBN, price, quantity );
}