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

Ok so i am having a hard time even getting started on this one, our teacher gave

ID: 3563753 • Letter: O

Question

Ok so i am having a hard time even getting started on this one, our teacher gave us no lecture notes or anything on these functions or how to use them and expected a program within a week. I'm frustrated and can't figure it out and was wondering if anyone could do this question.

The file posted here on contains a formatted list of integers, where each integer occupies one single line and takes 4 characters' space per line. You will note that this file currently contains 1 through 24, and 71 through 99. Write a C++ program using the seekg() and seekp() functions to insert the numbers 25 through 70 into this file while maintaining the original format (i.e., each integer occupies a single line and takes 4 characters' space). Below are a few useful tips:

Since I can't upload the txt file it looks ruffly like this, (you can make your own), file names is "file1.txt"

1 2 3 4 5 6 7 8 9 10 11...96 97 98 99

Intergers take up FOUR spaces.

Also very important, please limit yourself the direction given, I know it is very tempting to solve it using more advance functions in the library but please use what is given above in the directions. Also please comment your code so I can understand what is going on, I am actually trying to learn it just could not find any useful references online, thanks.

So the file should look like this(again i did not fill in all the numbers but general idea there)

http://imgur.com/PJcEfLS,xiVUA44#0

your file turned out like this, also a 5 after the 72.

http://imgur.com/PJcEfLS,xiVUA44#1

Explanation / Answer

#include<iostream>
#include<iomanip>
#include<fstream>
using namespace std;
int main()
{
fstream infile("input.txt",std::ios::in | std::ios::out ); // open file in both read and write mode.
if(!infile){
cout<<"unable to open file.so exiting from progrma.." << endl;
return 0;
}
int array[100]={0}; // create array to hold numbers after 24.
int index = 0;
while(!infile.eof()){ // start reading numbers from file.
int value ;
infile >> value;
if(value == 24) break; // got 24 break here.
}
long localtion_of_25 = infile.tellg(); // this is point to start writing numbers. store its location.
infile >> array[index]; // start reading numbers from 71 to array.
while(!infile.eof())
{
    index++;
    infile >> array[index];
}
infile.clear(); // clear file handle.
infile.seekp(localtion_of_25+2); // get back file handle to postion to start writing numbers from 25.
// added offset 2 because it takes 4 character spaces.
for(int i=25; i<=70; i++)
infile << left << setw(4) << i l; // start writing number from 25 to 70.
for(int i=0; i<index; i++) // start writing remaining numbers.
infile << left << setw(4)<< array[i] ;
infile.close(); // close file handle.
return 0;
}

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