Lab: Random access (binary) file input/output

Requirements

Modify the supplied program (raFileIO.cpp) to make it work properly. You will also need the file pres.dat

Program to be modified

/*
    raFileIO.cpp
    Name:
    CIS 150 Random access (binary) file I/O lab
    Date:

    Lab to demonstrate binary file reading and writing.
*/

// Finish the parts of the program marked ** TODO **
// A separate data file named pres.dat should also be downloaded.

#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <string>
using namespace std;

class President {
private:
    static const int NAME_SIZE = 39;
    int year;
    char name[NAME_SIZE+1];
public:
    President() {
        setYear(0);
        setName("");
    }
    President(int yr, string nm) {
        setYear(yr);
        setName(nm);
    }
    void setYear(int yr) {
        year = yr;
    }
    void setName(string nm) {
        if (nm.length() > NAME_SIZE) nm.resize(NAME_SIZE);
        nm.copy(name, nm.length());
        name[nm.length()] = '\0';
    }
    int getYear() const {
        return year;
    }
    string getName() const {
        return string(name);
    }
};

bool displayFile(string filename);

int main(int argc, char** argv) {
    string fname = "pres.dat";
    string pres44 = "Barack Obama";
    string pres45 = "Donald Trump";
    int year44 = 2009;
    int year45 = 2016;

    cout << "Before adding last two presidents:\n";
    bool ok = displayFile(fname);
    if (ok) {
        // ** TODO **  Part I
        // Open the file specified by fname for binary reading and writing
        // If file error, display error message and exit with failure status
        // Create a President object using year44 and pres44
        // Create a President object using year45 and pres45
        // Seek to the end of the file
        // Write both President records to the output file as binary data
        // Close the file
        // ** END OF TODO **
        cout << "\nAfter adding last two presidents:\n";
        displayFile(fname);
    }

    cout << "Press enter to continue...";
    cin.get();
    return 0;
}

bool displayFile(string filename) {
    long numRecs;
    President rec;
    
    // Open file for reading
    // ** TODO **  Part II
    // Open the file specified by fname for binary reading and writing
    // If file error, display error message and return false
    // Calculate the number of records and store in numRecs
    // For each record in file
    //     Read a binary record from the file into rec
    //     Display record number, ". ", name from record, " (", year from record, ")\n"
    // Close the file
    // ** END OF TODO **
    
    return true;
}

Example run of program

Before adding last two presidents:
1. George Washington (1789)
2. John Adams (1797)
3. Thomas Jefferson (1801)
4. James Madison (1809)
5. James Monroe (1817)
6. John Quincy Adams (1825)
7. Andrew Jackson (1829)
8. Martin Van Buren (1837)
9. William Henry Harrison (1841)
10. John Tyler (1841)
11. James K. Polk (1845)
12. Zachary Taylor (1849)
13. Millard Fillmore (1850)
14. Franklin Pierce (1853)
15. James Buchanan (1857)
16. Abraham Lincoln (1861)
17. Andrew Johnson (1865)
18. Ulysses S. Grant (1869)
19. Rutherford B. Hayes (1877)
20. James A. Garfield (1881)
21. Chester Arthur (1881)
22. Grover Cleveland (1885)
23. Benjamin Harrison (1889)
24. Grover Cleveland (1893)
25. William McKinley (1897)
26. Theodore Roosevelt (1901)
27. William Howard Taft (1909)
28. Woodrow Wilson (1913)
29. Warren G. Harding (1921)
30. Calvin Coolidge (1923)
31. Herbert Hoover (1929)
32. Franklin D. Roosevelt (1933)
33. Harry S. Truman (1945)
34. Dwight D. Eisenhower (1953)
35. John F. Kennedy (1961)
36. Lyndon B. Johnson (1963)
37. Richard Nixon (1969)
38. Gerald Ford (1974)
39. Jimmy Carter (1977)
40. Ronald Reagan (1981)
41. George Bush (1989)
42. Bill Clinton (1993)
43. George W. Bush (2001)

After adding last two presidents:
1. George Washington (1789)
2. John Adams (1797)
3. Thomas Jefferson (1801)
4. James Madison (1809)
5. James Monroe (1817)
6. John Quincy Adams (1825)
7. Andrew Jackson (1829)
8. Martin Van Buren (1837)
9. William Henry Harrison (1841)
10. John Tyler (1841)
11. James K. Polk (1845)
12. Zachary Taylor (1849)
13. Millard Fillmore (1850)
14. Franklin Pierce (1853)
15. James Buchanan (1857)
16. Abraham Lincoln (1861)
17. Andrew Johnson (1865)
18. Ulysses S. Grant (1869)
19. Rutherford B. Hayes (1877)
20. James A. Garfield (1881)
21. Chester Arthur (1881)
22. Grover Cleveland (1885)
23. Benjamin Harrison (1889)
24. Grover Cleveland (1893)
25. William McKinley (1897)
26. Theodore Roosevelt (1901)
27. William Howard Taft (1909)
28. Woodrow Wilson (1913)
29. Warren G. Harding (1921)
30. Calvin Coolidge (1923)
31. Herbert Hoover (1929)
32. Franklin D. Roosevelt (1933)
33. Harry S. Truman (1945)
34. Dwight D. Eisenhower (1953)
35. John F. Kennedy (1961)
36. Lyndon B. Johnson (1963)
37. Richard Nixon (1969)
38. Gerald Ford (1974)
39. Jimmy Carter (1977)
40. Ronald Reagan (1981)
41. George Bush (1989)
42. Bill Clinton (1993)
43. George W. Bush (2001)
44. Barack Obama (2009)
45. Donald Trump (2016)

Points (10 points total)