Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a short C++ program that opens a text file, write 2 integers to it, closes

ID: 3562631 • Letter: W

Question

  1. Write a short C++ program that opens a text file, write 2 integers to it, closes it, does the same with a binary file (open, write 2 integers to, and close), then opens the text file, read integers from into two different variables, and print them to the screen, and then does the same with the binary file. Basically, use the usual >>, << for text files, and read() and write() for binary files. For read () and write() you can use the sizeof() function to get the size of an integer. Remember, use different set of variables when reading from the files so you know that the values were actually read in.
  2. Create a class with two public members: an integer and a double. Repeat 3 and 4, but instead of 2 integers, use 2 objects of your class (put in whatever values you wish). You can use sizeof() to get the size of your object.
  3. Create an fstream to a binary file that you can both write to and read from. Write 5 objects of your class to your binary file. Then see if you can read them all back in order and print them to the screen. Next, see if you can retrieve the third object directly from the file by using seekg() and an offset from the file beginning of 2 object size.

Explanation / Answer

/* Testing Binary File IO (TestBinaryFileIO.cpp) */ #include #include #include #include using namespace std; int main() { string filename = "test.bin"; // Write to File ofstream fout(filename.c_str(), ios::out | ios::binary); if (!fout.is_open()) { cerr
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote