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

1. Write a short C++ program that: A. Opens a text file B. Writes 2 intergers to

ID: 668127 • Letter: 1

Question

1. Write a short C++ program that:

A. Opens a text file

B. Writes 2 intergers to the text file

C. Closes the text file

It should then repeat steps A-C with a binary files(open, write 2 intergers to, and close).

Then the program should

A. Opens the text file

B. Read the 2 intergers from the text file

C. Closes the text file

D. Prints the 2 intergers to the screen.

It should then repeat steps A-D with the binary file(open, read 2 intergers, close, print).

Use the usual >>,<< for text files, and read() & write() for binary files. For read() and write() you can use the sizeof() function to get the size of an integer. Use different sets of variables when reading from the files so you know that the values were actually read in.

2. Create a class called Student_Record with two public members: an integer (ID) and a double (total_Points). Repeat problem 1, but Instead of using 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 yo 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

#include<iostream.h>
#include<conio.h>
#include<fstream.h>

void main()
{
   ofstream fout;

   fout.open("data");
   fout<<" 25";
   fout<<" 30";


   fout.close();
  
   ifstream fin;
   int n1,n2;
   fin>>n1;
   fin>>n2;
   cout<<n1;
   cout<<n2;
   fin.close();

   std::ofstream fs("example.bin", std::ios::out | std::ios::binary | std::ios::app);
   int a=45,b=50;
   fs.write(a, sizeof a);
   fs.write(b, sizeof b);
   fs.close();
  
   streampos begin,end;
   ifstream myfile ("example.bin", ios::binary);
   begin = myfile.tellg();
   myfile.seekg (0, ios::end);
   end = myfile.tellg();
   myfile.close();

}