Question

In: Computer Science

ok here is my homework This assignment focuses on file streams. Your program will accept the...

ok here is my homework

This assignment focuses on file streams.

Your program will accept the name of an input file from the user, and the name of an output file from the user. You may assume the input file contains no more than 100 lines.

Your program will write the lines of the input file to the output file in reverse line order, i.e. the first line of the input file becomes the last line of the output file, the second line of the input file becomes the second-to-the-last line of the output file, and so forth. Note that the lines themselves are not reversed, only the order in which the lines appear.

Solutions

Expert Solution

NOTE : FEEL FREE TO ASK ANY DOUBTS IN THE COMMENT SECTION

input.txt

CODE

#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
int main(){
   // variables to store file names
   char in[20],out[20];
   // filestream variables
   fstream infile,outfile;
   cout << "\n Enter Input Filename: ";
   cin >> in;   // reading input file name
   cout << "\n Enter Output Filename: ";
   cin >> out;// reading output file name
   // opening infile in read mode
   // opening outfile in write mode
   infile.open(in,ios::in);
   outfile.open(out,ios::out);
   // if unable to open file showing error
   // and also exit from program
   if(!infile.is_open() || !outfile.is_open()){
       cout << "Error while Opening or Creating file";
       return 0;
   }
   int count=0;   // variable to store current file lines count
   string line="";// to store line
   // Loop until file does not reach end of file
   while(getline(infile,line))
   {
       // increase line count
       count = count + 1;
   }
   // closing file and opens again in read mode
   infile.close();
   infile.open(in,ios::in);
   // string array to store lines
   string lines[count];
   // starting index of array from end
   int index=count-1;
   // Loop until doesnot reaches end of file
   while(getline(infile,line)){
       // adding line to lines array at index
       lines[index]=line+"\n";
       index=index-1; // decrementing index
   }
   // writing lines in reverse order to outfile
   for(int i=0;i<count;i++){
       outfile << lines[i];// writing to file
   }
   // Closing infile adn outfile
   infile.close();
   outfile.close();
   cout << "\n Data Successfully Inserted";
   return 0;
}

OUTPUT in CONSOLE

output.txt

CODE in EDITOR

DEAR SIR, PLEASE CONSIDER MY EFFORT AND PLEASE DON'T FORGET TO GIVE AN UP VOTE

Thank YOU :-)


Related Solutions

Here is my assignment: Write a program that generates a random number between 1 and 50...
Here is my assignment: Write a program that generates a random number between 1 and 50 and asks the user to guess it. As a hint, it tells the user how many divisors it has, (excluding 1 and the number itself). Each time the user makes a wrong guess, it displays "Wrong" and says if the guess was too low or too high, until the user gets it right, in which case it says "Right" and says how many trials...
This question is in my plant physiology homework and the assignment is on the electron transport...
This question is in my plant physiology homework and the assignment is on the electron transport chain. How is the H+ gradient (potential energy) used? What are the products that the proton gradient made? Where in the chloroplast are they made? (3 points)
This isn't a homework assignment more of a search for advice. The lab experiment that my...
This isn't a homework assignment more of a search for advice. The lab experiment that my group is conducting is to separate two mixtures, a solid-solid and a liquid-liquid mixture. Each mixture has 2 separate components. The solid-solid mixture was easy enough, a quick solvent extraction completely separated the two and soon we will do IR, NMR and melting point determination to try to identify the solids. The liquids however are another problem all in themselves. The mixture is tinted...
Java Write a program that will only accept input from a file provided as the first...
Java Write a program that will only accept input from a file provided as the first command line argument. If no file is given or the file cannot be opened, simply print “Error opening file!” and stop executing. A valid input file should contain two lines. The first line is any valid string, and the second line should contain a single integer. The program should then print the single character from the string provided as the first line of input...
""""" I need new post to accepted my homework """"" Assignment Questions: There are four macro...
""""" I need new post to accepted my homework """"" Assignment Questions: There are four macro strategies to create and deliver value to customers and that can help a firm to develop a sustainable competitive advantage. Think of companies from the local market who you are very loyal to in many categories like (food, electronics, personal care etc.). Is it the product, location, operational, or customer excellence that you are loyal to that firm? Critically examine. (2.5 Marks) You have...
Hi, so for my homework assignment, we are meant to change this function so that instead...
Hi, so for my homework assignment, we are meant to change this function so that instead of making a list from the array going up in an ascending order, making it be listed in descending order, meaning going from largest number to smallest number, the code I have now works for the ascending order, but how would I change this for it to go descending? Any help would be amazing, and please with a tutorial, thanks so much. def heapify(arr,...
The purpose of this homework is to test your knowledge of File IO and Collections and...
The purpose of this homework is to test your knowledge of File IO and Collections and the language would be java. Suppose that we have a file Employee.csv which contains employee information of a certain company. The first line of the file contains the following header: employeeId,firstName,lastName,dob,gender,position After the header line, each employee information is written on a separate line which consists of 6 comma-separated fields, for example:1234567, John, Smith,20/12/1989, M, Software Engineer Question 1. Write code to read the...
Validating Input file, So for my C++ assignment, my professor says we need to validate the...
Validating Input file, So for my C++ assignment, my professor says we need to validate the Input File, Which means, if the line is missing a comma, one of the 3 information, or the string is a white line, it will ignore it. If the line has a white line, the program will correct it and will read in the line. I will store these into an Array, and display them in First name, Last name, and Number of Votes...
ASSIGNMENT: Write a program and use the attached file (babynames.txt) as input file, and create two...
ASSIGNMENT: Write a program and use the attached file (babynames.txt) as input file, and create two output tiles. One file listing out all boys names, and the other file listing out all girls name. CODE: (teacher gave some of the code below use it to find the answer please String B is the boy names String E is girl names) import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Scanner; /** This program reads a file with numbers, and writes the numbers...
ASSIGNMENT: Write a program and use the attached file (babynames.txt) as input file, and create two...
ASSIGNMENT: Write a program and use the attached file (babynames.txt) as input file, and create two output tiles. One file listing out all boys names, and the other file listing out all girls name. CODE: (teacher gave some of the code below use it to find the answer please String B is the boy names String E is girl names) import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Scanner; /** This program reads a file with numbers, and writes the numbers...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT