![]() |
|
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
[an error occurred while processing this directive]
Steps
How It Works After the two source comment lines, you see the include directive for iostream. On the next line is the using directive, effectively making all names visible in namespace std into the current scope. The next three lines of code declare and define three constant values. const int DB_OK = 0 ; const int DB_NOT_INIT = -1 ; const int DB_ACCESS_ERROR = -2 ; These values are used as status codes for the dummy database functions. These values are representative of some well-known Indexed Sequential Access Methods (ISAM) C libraries. Not that the values themselves match the libraries codes, but rather the logic of using negative integer values to represent error codes. The next four lines int dbInit( ) ; int dbOpen( const char *fname ) ; int dbGetData( int field, char *data ) ; int dbClose( int handle ) ; are function declarations representing traditional C-style ISAM functions. This program does not actually access any data files, but simulates the logic of doing so. The functions are described in more detail later in this section. The next four source lines void dbInitEH( ) ; int dbOpenEH( const char *fname ) ; void dbGetDataEH( int field, char *data ) ; void dbCloseEH( int handle ) ; declare four functions oriented around exception handling. The function names match the previous function names with the string EH appended to the names. The EH designates that these functions utilize exception handling. Also, notice that all but one of these functions return void, whereas the previous four functions return an int. Now, lets take a step back and examine the following section of code. Notice all the if-else statements; this code is almost hard to follow. You really have to pay attention to what is happening here. if( dbInit( ) == DB_OK ) { int theHandle = dbOpen( test.dat ) ; if( theHandle > DB_OK ) { char data[ 30 ] ; if( dbGetData( 1, data ) == DB_OK ) cout << Data is: << data << endl ; else cout << Error getting data << endl ; if( dbClose( theHandle ) != DB_OK ) cout << Error closing DB << endl ; } else cout << Error opening DB << endl ; } else cout << Error initializing DB << endl ; The first line in this section of code calls the dbInit() function to initialize the database. The return value from that function specifies whether the initialization is successful. Otherwise, the associated else reports an error.
|
![]() |
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.
|