示例#1
0
// create account
void ClientData::createAccount ()
{
	int accountNumber;
	int pinCode;
	string UserName;
	
	double balance;

    fstream outdatabase( "ATMData.txt", ios::in | ios::out | ios::binary );

// exit program if fstream cannot open file
 if (!outdatabase)
    {
   cerr << "File could not be opened." << endl;
   exit( 1 );
   } // end if

  cout << "Enter account number (1 to 10000, 0 to end input)\n? ";

 // require user to specify account number
   ClientData database;
   cin >> accountNumber;

 // user enters information, which i  copied into file
while ( accountNumber > 0 && accountNumber <= 10000 )
 {
// user enters pin code, user name, and balance
cout << "Enter Pin code, User name,  balance\n? ";
 
 cin >> pinCode;
 cin >>UserName;

 cin >> balance;

 // set record accountNumber, pincode, UserName, balance values
   database.setAccountNumber( accountNumber ) ;
   database.setPinCode(pinCode);
   database.setUserName( UserName );
   //database.setFirstName( firstName ) ;
   database.setBalance( balance );

// seek position in file of user-specified record
outdatabase.seekp( ( database.getAccountNumber() - 1 ) *
sizeof( ClientData ) ) ;

 // enable user to enter another account
 cout << "Enter account number\n? ";
 cin >> accountNumber;
 } // end while
 } // end save data to file
示例#2
0
int main()
{
   int accountNumber;
   string lastName;
   string firstName;
   double balance;

   fstream outCredit( "credit.dat", ios::in | ios::out | ios::binary );

   // exit program if fstream cannot open file
   if ( !outCredit ) 
   {
      cerr << "File could not be opened." << endl;
      exit( EXIT_FAILURE );
   } // end if

   cout << "Enter account number (1 to 100, 0 to end input)\n? ";

   // require user to specify account number
   ClientData client;
   cin >> accountNumber;

   // user enters information, which is copied into file
   while ( accountNumber > 0 && accountNumber <= 100 ) 
   {
      // user enters last name, first name and balance
      cout << "Enter lastname, firstname, balance\n? ";
      cin >> lastName;
      cin >> firstName;
      cin >> balance;

      // set record accountNumber, lastName, firstName and balance values
      client.setAccountNumber( accountNumber );
      client.setLastName( lastName );
      client.setFirstName( firstName );
      client.setBalance( balance );

      // seek position in file of user-specified record
      outCredit.seekp( ( client.getAccountNumber() - 1 ) * 
         sizeof( ClientData ) );

      // write user-specified information in file
      outCredit.write( reinterpret_cast< const char * >( &client ),
         sizeof( ClientData ) );

      // enable user to enter another account
      cout << "Enter account number\n? ";
      cin >> accountNumber;
   } // end while
} // end main