Question

Retrieve program randomAccess.cpp and the data file proverb.txt from the Lab 14 folder. The code is as follows: #include...

Retrieve program randomAccess.cpp and the data file proverb.txt from the Lab 14 folder. The code
is as follows:
#include <iostream>
#include <fstream>
#include <cctype>
using namespace std;
327
int main()
{
fstream inFile("proverb.txt", ios::in);
long offset;
char ch;
char more;
do
{
// Fill in the code to write to the screen
// the current read position (with label)
cout << "Enter an offset from the current read position: ";
cin >> offset;
// Fill in the code to move the read position "offset" bytes
// from the current read position.
// Fill in the code to get one byte of information from the file
// and place it in the variable "ch".
cout << "The character read is " << ch << endl;
cout << "If you would like to input another offset enter a Y"
<< endl;
cin >> more;
// Fill in the code to clear the eof flag.
} while (toupper(more) == 'Y');
inFile.close();
return 0;
}
Exercise 1: Fill in the code as indicated by the comments in bold. The file proverb.txt contains the
following information:
Now Is The Time fOr All GoOd Men to come to the aid of their Family
Sample Run:
The read position is currently at byte 0
Enter an offset from the current read position: 4
The character read is I
If you would like to input another offset enter a Y
y
The read position is currently at byte 5
Enter an offset from the current read position: 2
The character read is T
If you would like to input another offset enter a Y
y
328
The read position is currently at byte 8
Enter an offset from the current read position: 6
The character read is e
If you would like to input another offset enter a Y
y
The read position is currently at byte 15
Enter an offset from the current read position: 44
The character read is r
If you would like to input another offset enter a Y
y
The read position is currently at byte 60
Enter an offset from the current read position: 8
The character read is r
If you would like to input another offset enter a Y
N
Exercise 2: Why do you think that the character printed at the last run was another 4? What
wouldyou have to do to get a different letter after the position is beyond the eof marker?

Exercise 3: Change the program so that the read position is calculated from the end of the file. What
type of offsets would you need to enter to get characters from the proverb? Do several sample runs
with different numbers to test your program.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/******************************************************************************

* Programe to read a file and write the content of input file into another file

*******************************************************************************/

#include<iostream>

#include<fstream>

#include<cstdlib>

using namespace std;

/* Function prototype */

void get_stream(ifstream& in_s,ofstream& out_s);

/* Definition of main function */

int main(){

char c;

//Declaration of the stream of type input and output

ifstream in_s;

ofstream out_s;

//Function call

get_stream(in_s,out_s);

while(!in_s.eof()){

in_s.get(c); //read all characters of input file one by one

out_s.put(c); //write all characters of input file one by one into output file

}

in_s.close(); //closing the input file

out_s.close(); //closing the output file

return 0;

}

void get_stream(ifstream& in_s,ofstream& out_s){

char input_file[15];

char output_file[15];

cout<<"Enter the name of the input file:";

cin>>input_file;

cout<<"Enter the name of the output file:";

cin>>output_file;

in_s.open(input_file);

if(in_s.fail())

{

cout<<"Unable to open input file "<<input_file<<endl;

exit(EXIT_FAILURE);

}

out_s.open(output_file);

if(out_s.fail())

{

cout<<"Unable to open output file "<<input_file<<endl;

exit(EXIT_FAILURE);

}

return;

}

@ubuntu:~/progra g+ Lab9B.cpp aubuntu:~/program$ ./a.out Enter the name of the input file:act9B.txt Enter the name of the out

Add a comment
Know the answer?
Add Answer to:
Retrieve program randomAccess.cpp and the data file proverb.txt from the Lab 14 folder. The code is as follows: #include...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need to complete a fill-in-the-blank C++ program using DEV C++, code needs to stick to program...

    ************************* proverb.txt. **********************************Now Is The Time fOr All GoOd Men to come to the aid of their Family*************************************************************************the sample run is as follows:Sample Run:The read position is currently at byte 0Enter an offset from the current position: 4The character read is IIf you would like to input another offset enter a Y yThe read position is currently at byte 5Enter an offset from the current position: 2The character read is TIf you would like to input another offset enter a...

  • Please use my Lab 3.2 code for this assignment Lab 4.2 instruction Using the code from...

    Please use my Lab 3.2 code for this assignment Lab 4.2 instruction Using the code from lab 4.1, add the ability to read from a file. Modify the input function:       * Move the input function out of the Cargo class to just below the end of the Cargo class       * At the bottom of the input function, declare a Cargo object named         temp using the constructor that takes the six parameters.       * Use the Cargo output...

  • Why is my code not calculating the pay and not printing entire data at the end? // // main.cpp // Project 14 // // Created by Esmeralda Martinez on 5/13/19. // Copyright © 2019 Esmeralda Martinez. All...

    Why is my code not calculating the pay and not printing entire data at the end? // // main.cpp // Project 14 // // Created by Esmeralda Martinez on 5/13/19. // Copyright © 2019 Esmeralda Martinez. All rights reserved. // #include<iostream> #include<fstream> #include<cstdlib> #include<regex> #include <iomanip> using namespace std; float grossPay(float hrsWorked, float payrate); float grossPay(float hrsWorked, float payrate){ return hrsWorked*payrate;       } int main(){    //opening an output file ifstream outFile("Employee.txt", ios::in); //Read variables string depId,emp_num,firstName,lastName,email,hrs_worked,pay_rate,ch="y";    //Regex patterns regex integerPattern("(\\+|-)?[[:digit:]]+"); regex...

  • Lab 4.1 Utilizing the code from Lab 3.2, add an overloaded operator == which will be...

    Lab 4.1 Utilizing the code from Lab 3.2, add an overloaded operator == which will be used to compare two objects to see if they are equal. For our purposes, two objects are equal if their abbreviation and uldid are the same. You’ll need to make your overloaded operator a friend of the Cargo class. Create a unit1 object and load it with this data: uld – Pallet abbreviation – PAG uldid – PAG32597IB aircraft - 737 weight – 3321...

  • Hey, so i am trying to have my program read a text file using a structure...

    Hey, so i am trying to have my program read a text file using a structure but i also want to be able to modify the results(I kinda have this part but it could be better). I cant seem to get it to read the file(all the values come up as 0 and i'm not sure why because in my other program where it wrote to the txt file the values are on the txt file) i copied and pasted...

  • Can you tell me what is wrong and fix this code. Thanks #include <iostream> #include <string>...

    Can you tell me what is wrong and fix this code. Thanks #include <iostream> #include <string> #include <fstream> #include <sstream> using namespace std; //function void displaymenu1(); int main ( int argc, char** argv ) { string filename; string character; string enter; int menu1=4; char repeat; // = 'Y' / 'N'; string fname; string fName; string lname; string Lname; string number; string Number; string ch; string Displayall; string line; string search; string found;    string document[1000][6];    ifstream infile; char s[1000];...

  • Lab 5.1 C++ Utilizing the code from Lab 4.2, replace your Cargo class with a new...

    Lab 5.1 C++ Utilizing the code from Lab 4.2, replace your Cargo class with a new base class. This will be the base for two classes created through inheritance. The Cargo class will need to have virtual functions in order to have them redefined in the child classes. You will be able to use many parts of the new Cargo class in the child classes since they will have the same arguments/parameters and the same functionality. Child class one will...

  • #include <fstream> #include <iostream> #include <cstdlib> using namespace std; // Place charcnt prototype (declaration) here int...

    #include <fstream> #include <iostream> #include <cstdlib> using namespace std; // Place charcnt prototype (declaration) here int charcnt(string filename, char ch); int main() { string filename; char ch; int chant = 0; cout << "Enter the name of the input file: "; cin >> filename; cout << endl; cout << "Enter a character: "; cin.ignore(); // ignores newline left in stream after previous input statement cin.get(ch); cout << endl; chcnt = charcnt(filename, ch); cout << "# of " «< ch« "'S:...

  • If necessary, create a new project named Intermediate24 Project and save it in the Cpp8\Chap14 folder....

    If necessary, create a new project named Intermediate24 Project and save it in the Cpp8\Chap14 folder. Also create a new source file named Intermediate24.cpp. If you are using Microsoft Visual C++, copy the Intermediate24.txt file from the Cpp8\Chap14 folder to the Cpp8\Chap14\Intermediate24 Project folder. Use a text editor to open the Intermediate24.txt file, which contains payroll codes and salaries. Close the Intermediate24.txt file. Create a program that allows the user to enter a payroll code. The program should search for...

  • im writing a c++ code and getting stuck on two parts. The first part, when I...

    im writing a c++ code and getting stuck on two parts. The first part, when I go to write the customer order out to the file, it writes everything but the price out right. I'll get a weird number like 5.95828e-039 or nan. When I printout to the console it works fine so not sure what's wrong. Second After I write the order out to the file, I then have to go in and read the file and calculate the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT