Creating a randomly accessed file. - C++ File Stream

C++ examples for File Stream:stream

Description

Creating a randomly accessed file.

Demo Code

                                                                                                                                                                                                                    
#include <string> 
#include <cstdlib>
#include <fstream>
#include <iostream>
                                                                                                                                                                                                                    
class Employee {/*  w ww.  java  2s .  c o m*/
 public:
    // default Employee constructor
    Employee(int = 0, std::string = "", std::string = "", double = 0.0f);
                                                                                                                                                                                                                    
    // accessor functions for accountNumber
    void setAccountNumber(int);
    int getAccountNumber() const;
                                                                                                                                                                                                                    
    // accesssor functions for lastName
    void setLastName(std::string);
    std::string getLastName() const;
                                                                                                                                                                                                                    
    // accessor functions for firstName
    void setFirstName(std::string);
    std::string getFirstName() const;
                                                                                                                                                                                                                    
    // accessor functions for balance
    void setBalance(double);
    double getBalance() const;
                                                                                                                                                                                                                    
 private:
    int accountNumber;
    char lastName[15];
    char firstName[10];
    double balance;
};
                                                                                                                                                                                                                    
#include <string>
                                                                                                                                                                                                                    
// default Employee constructor
Employee::Employee(int accountNumberValue, std::string lastNameValue,
                       std::string firstNameValue, double balanceValue) {
    setAccountNumber(accountNumberValue);
    setLastName(lastNameValue);
    setFirstName(firstNameValue);
    setBalance(balanceValue);
}
int Employee::getAccountNumber() const { return accountNumber; }
void Employee::setAccountNumber(int accountNumberValue) {
    accountNumber = accountNumberValue;
}
std::string Employee::getLastName() const { return lastName; }
void Employee::setLastName(std::string lastNameString) {
    int length = lastNameString.size();
    length = (length < 15 ? length : 14);
    lastNameString.copy(lastName, length);
    lastName[length] = '\0';
}
std::string Employee::getFirstName() const { return firstName; }
void Employee::setFirstName(std::string firstNameString) {
    int length = firstNameString.size();
    length = (length < 10 ? length : 9);
    firstNameString.copy(firstName, length);
    firstName[length] = '\0';
}
double Employee::getBalance() const { return balance; }
void Employee::setBalance(double balanceValue) { balance = balanceValue; }
                                                                                                                                                                                                                    
                                                                                                                                                                                                                    
int main(int argc, const char* argv[]) {
    std::ofstream outCredit("credit.dat", std::ios::out | std::ios::binary);
                                                                                                                                                                                                                    
    // exit program if ofstream could not open file
    if (!outCredit) {
        std::cerr << "File could not be opened." << std::endl;
        return 1;
    }
                                                                                                                                                                                                                    
    Employee blankClient;  // constructor zeros out each data member
                                                                                                                                                                                                                    
    // output 100 blank records to file
    for (int i = 0; i < 100; ++i) {
        outCredit.write(reinterpret_cast<const char*>(&blankClient),
                        sizeof(Employee));
    }
                                                                                                                                                                                                                    
    return 0;
}

Related Tutorials