|
|
|
|
C
|
101 lines
|
75 sloc
|
2.566 kB
  1. #define DATABASENAME "database.bin" 
  2. #include <stdio.h> 
  3. #include <stdlib.h> 
  4. #include <string.h> 
  5.  
  6. typedef struct _record{ 
  7.   char id[50]; 
  8.   char fname[50]; 
  9.   char lname[50]; 
  10.   char mname[50]; 
  11.   char suffix[50]; 
  12.   char bdate[50]; 
  13.   char gender[50]; 
  14.   char ssnum[50]; 
  15.   char add1[50]; 
  16.   char add2[50]; 
  17.   char zip[50]; 
  18.   char maid[50]; 
  19.   char mrn[50]; 
  20.   char city[50]; 
  21.   char state[50]; 
  22.   char phone1[50]; 
  23.   char phone2[50]; 
  24.   char email[50]; 
  25.   char alias[50]; 
  26. } record; 
  27.  
  28. record tmp; 
  29.  
  30. int getRecord( char file[], int line, record *structure ){ 
  31.      
  32.   FILE * handle; 
  33.    
  34.   handle = fopen( file, "rb+" ); 
  35.    
  36.   fseek( handle, sizeof( record ) * ( line ), SEEK_SET ); 
  37.    
  38.   fread( structure, 1, sizeof( record ), handle);  
  39.     
  40.   return 1; 
  41.  
  42. }; 
  43.  
  44. int changeRecord( char file[], int line, char header[], char value[] ){ 
  45.     
  46.   FILE * handle; 
  47.    
  48.   record tmp; 
  49.  
  50.   memset(&tmp, 0, sizeof(tmp)); 
  51.  
  52.   handle = fopen( file, "rb+" ); 
  53.    
  54.   fseek( handle, sizeof( record ) * ( line ), SEEK_SET ); 
  55.  
  56.   fread( &tmp, 1, sizeof( record ), handle ); 
  57.   
  58.    //Modify tmp here 
  59.    
  60.   fseek( handle, sizeof( record ) * ( line ), SEEK_SET ); 
  61.  
  62.   fwrite( &tmp, sizeof( record ), 1, handle ); 
  63.    
  64.   return 1; 
  65.     
  66. }; 
  67.  
  68. void showRecord( record *dataArray ){ 
  69.    
  70.   printf( "---------------------------------"); 
  71.   printf( "ID: %s\n",          dataArray->id     );  
  72.   printf( "Last name: %s\n",   dataArray->lname  );  
  73.   printf( "First name: %s\n",  dataArray->fname  );  
  74.   printf( "Middle name: %s\n", dataArray->mname  );  
  75.   printf( "Suffix: %s\n",      dataArray->suffix );  
  76.   printf( "Birth Date: %s\n",  dataArray->bdate  );  
  77.   printf( "Gender: %s\n",      dataArray->gender );  
  78.   printf( "SS Num: %s\n",      dataArray->ssnum  );  
  79.   printf( "Address 1: %s\n",   dataArray->add1   );  
  80.   printf( "Address 2: %s\n",   dataArray->add2   );  
  81.   printf( "Zip: %s\n",         dataArray->zip    );  
  82.   printf( "Maiden: %s\n",      dataArray->maid   );  
  83.   printf( "MRN: %s\n",         dataArray->mrn    );   
  84.   printf( "City: %s\n",        dataArray->city   );  
  85.   printf( "State: %s\n",       dataArray->state  ); 
  86.   printf( "Phone 1: %s\n",     dataArray->phone1 );  
  87.   printf( "Phone 2: %s\n",     dataArray->phone2 );  
  88.   printf( "Email: %s\n",       dataArray->email  );  
  89.   printf( "Alias: %s\n",       dataArray->alias  );  
  90.  
  91. } 
  92.  
  93. int main() { 
  94.  
  95.    if ( loadDatabase( DATABASENAME ) ) { 
  96.       getRecord(DATABASENAME, 1, &tmp); 
  97.       showRecord(&tmp); 
  98.       changeRecord(DATABASENAME, 1, "lname", "Last"); 
  99.    }; 
  100.    return 0; 
  101. };