    01: #include <iostream>
    02: #include <iomanip>
    03: #include <fstream>
    04: #include <sstream>
    05:
    06: using namespace std;
    07:
    08: #include "ccc_empl.cpp"
    09:
    10: const int NEWLINE_LENGTH = 2; /* or 1 on Unix */
    11: const int RECORD_SIZE = 30 + 10 + NEWLINE_LENGTH;
    12:
    13: /**
    14:    Converts a string to a floating-point value, e.g.
    15:    "3.14" -> 3.14.
    16:    @param s a string representing a floating-point value
    17:    @return the equivalent floating-point value
    18: */
    19: double string_to_double(string s)
    20: {
    21:    istringstream instr(s);
    22:    double x;
    23:    instr >> x;
    24:    return x;
    25: }
    26:
    27: /**
    28:    Raises an employee salary.
    29:    @param e employee receiving raise
    30:    @param percent the percentage of the raise
    31: */
    32: void raise_salary(Employee& e, double percent)
    33: {
    34:    double new_salary = e.get_salary() * (1 + percent / 100);
    35:    e.set_salary(new_salary);
    36: }
    37:
    38: /**
    39:    Reads an employee record from a file.
    40:    @param e filled with the employee
    41:    @param in the stream to read from
    42: */
    43: void read_employee(Employee& e, istream& in)
    44: {
    45:    string line;
    46:    getline(in, line);
    47:    if (in.fail()) return;
    48:    string name = line.substr(0, 30);
    49:    double salary = string_to_double(line.substr(30, 10));
    50:    e = Employee(name, salary);
    51: }
    52:
    53: /**
    54:    Writes an employee record to a stream
    55:    @param e the employee record to write
    56:    @param out the stream to write to
    57: */
    58: void write_employee(Employee e, ostream& out)
    59: {
    60:    out << e.get_name()
    61:       << setw(10 + (30 - e.get_name().length()))
    62:       << fixed << setprecision(2)
    63:       << e.get_salary()
    64:       << "\n";
    65: }
    66:
    67: int main()
    68: {
    69:    cout << "Please enter the data file name: ";
    70:    string filename;
    71:    cin >> filename;
    72:    fstream fs;
    73:    fs.open(filename.c_str());
    74:    fs.seekg(0, ios::end); /* go to end of file */
    75:    int nrecord = fs.tellg() / RECORD_SIZE;
    76:
    77:    cout << "Please enter the record to update: (0 - "
    78:       << nrecord - 1 << ") ";
    79:    int pos;
    80:    cin >> pos;
    81:
    82:    const double SALARY_CHANGE = 5.0;
    83:
    84:    Employee e;
    85:    fs.seekg(pos * RECORD_SIZE, ios::beg);
    86:    read_employee(e, fs);
    87:    raise_salary(e, SALARY_CHANGE);
    88:    fs.seekp(pos * RECORD_SIZE, ios::beg);
    89:    write_employee(e, fs);
    90:
    91:    fs.close();
    92:    return 0;
    93: }