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

A data file contains text from an online publication. It may be considered to be

ID: 3771167 • Letter: A

Question

A data file contains text from an online publication. It may be considered to be a binary file. A program is required to read the 30 characters in the middle of the file and replace them with the author’s information.

a.   If the file is called book.dat, how might we open the file so that the resulting stream fs can read and write the file in binary?
(1 mark)

b. Write the code to get the number of bytes in the file and store the
value in the variable fileSize.
(1 mark)

c.   To read in just the centre of the file, the following pseudocode is developed:

1. Position read marker 15 characters before the middle of the file
2. Read in 30 characters in to the char array middle[30] .

Write the two C++ statements needed to perform these tasks. You may assume that middle is already defined.
(1 Mark)

d. The author’s information is contained within a struct Details.
struct Details
{
char Name[14];
int AuthID;
int PubId;
};

Details author= {“W.Gibson”, 17394,218};

Write the line of code necessary to write author to fs.You may assume that the write marker is already positioned in the correct location.

THIS IS A C++ PROBLEM

Explanation / Answer


// obtaining file size
#include <iostream>
#include <string>
#include <cstring>
#include <fstream>
#include <sstream>
using namespace std;

struct Details
{
char Name[14];
int AuthID;
int PubId;
};


void addAuthorInfoMiddle(long pos, string filename, char replacement[])
{
   std::ofstream out(filename.c_str(),ios::binary | ios::out | ios::in);
   cout << "POS: "<< pos <<endl;;
   out.seekp(pos,ios::beg);

   cout << out.tellp() << endl;
   out.write(replacement, strlen(replacement));
   out.close();
}

string toString(int number) {
   stringstream strs;
   strs << number;
   string temp_str = strs.str();
   return temp_str;
}

int main () {

streampos begin,end;

string filename = "D:/ravi/Cheg/example.txt";
// Answer for : a.   If the file is called book.dat, how might we open the file so that the resulting stream fs can read and write the file in binary?
ifstream myfile (filename.c_str(), ios::binary); //open in binary mode

//Answer for : b. Write the code to get the number of bytes in the file and store the value in the variable fileSize.
begin = myfile.tellg();
myfile.seekg (0, ios::end);
end = myfile.tellg();
myfile.close();
cout << "size is: " << (end-begin) << " bytes. ";
long filesize = end - begin;

//c. & d
Details author= {"W.Gibson", 17394,218};
char middle[30];

int len = strlen(author.Name);
for(int i = 0; i < len; i++)
      middle[i] = author.Name[i];

string str = " ("+toString(author.AuthID)+", "+toString(author.PubId)+") ";
char *chs = (char*) str.c_str();
   int len2 = strlen(chs);
    for(int i = 0; i < len2; i++)
      middle[len+i] = chs[i];

    //replace the author name
    addAuthorInfoMiddle((filesize/2)-15, filename, middle);


return 0;
}

---output----------------

Input ifle:

ios::begios::begios::begios::begios::begios::begios::begios::begios::begios::begios::begios::begios::begios::begios::begios::begios::beg
ios::begios::begios::begios::begios::begios::beg

ios::begios::begios::beg
ios::beg
ios::begios::beg
ios::begios::beg
ios::beg

Afetr modiging:

ios::begios::begios::begios::begios::begios::begios::begios::begios::begios::begios::begios::begios::begios::begios::begW.Gibson (17394, 218) :begios::begios::begios::begios::begios::beg

ios::begios::begios::beg
ios::beg
ios::begios::beg
ios::begios::beg
ios::beg