Question

Write a C++ program that reverses a file. More specifically, given an input file (input.txt) your...

Write a C++ program that reverses a file. More specifically, given an input file (input.txt) your program will reverse every line in the input. This program does not have an output file, as it modifies the input file directly. You may assume that your input file has 1000 lines or less, if this helps.

sample input:

Hello World!

I study C++

following output:

!dlroW olleH

++C yduts I

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

input.txt(Save the file under D Drive.Then the path of th efile pointing to it is D:\\input.txt )

Hello World!
I study C++

____________________

#include <iostream>
#include <cmath>
#include <fstream>
#include <string>

using namespace std;

int main()
{
//defines an input stream for the data file  
ifstream dataIn;
  
//Declaring variables
string rev_string="";
string str;
  
//Opening the input file
dataIn.open("D:\\input.txt");
  
/* This loop will get each line from
* the file and reverse it and displayed
* on the console
*/
while(getline(dataIn, str,'\n') )
{
   rev_string="";
   //This for loop will reverse the string
for(int i=str.length()-1;i>=0;i--)
{
rev_string+=str.at(i);
}
//Displaying the output
cout<<rev_string<<endl;

}
  
  
return 0;
}

____________________

_______________Thank You

Add a comment
Know the answer?
Add Answer to:
Write a C++ program that reverses a file. More specifically, given an input file (input.txt) your...
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
  • Write a C Program that: - Creates an input file (input.txt) - Take user inputted integer...

    Write a C Program that: - Creates an input file (input.txt) - Take user inputted integer for number of lines in the input file - accepts inputted text from command line to the input file (input.txt) - saves and closes file. (input.txt) - reopens and displays the newly inputted contents of the file

  • In c programming, I need to write a program that reverses each of the lines in...

    In c programming, I need to write a program that reverses each of the lines in a file. The program accepts two command line arguments: The first one is the name of the input file The second is the name of the output file If the user didn't give two filenames, display an error message and exit. The program reads in each line of the input file and writes each line in reverse to the output file. Note, the lines...

  • Use PYTHON Write a program that will take an input file called input.txt which will contain a Jav...

    use PYTHON Write a program that will take an input file called input.txt which will contain a Java program and parse it and output an annotated version. Your program will track the nesting depth of the braces of the input file and will output an annotated version of the file. Your final program will 1. List the nesting depth of curly braces 2. Ignore braces inside quotes or comments Use Python Assume that all quoted strings begin and end on...

  • Write a program that reverses a text file by using a stack. The user interface must...

    Write a program that reverses a text file by using a stack. The user interface must consist of 2 list boxes and 3 buttons. The 3 buttons are: Read - reads the text file into list box 1. Reverse - reverses the items in list box 1 by pushing them onto stack 1, then popping them from stack 1 (in the reverse order) and adding them to list box 2. Write - writes the contents of list box 2 to...

  • Write a program that takes in a line of text as input, and outputs that line of text in reverse.

    Write a program that takes in a line of text as input, and outputs that line of text in reverse. The program repeats, ending when the user enters "Quit", "quit", or "q" for the line of text.Ex: If the input is:Hello there Hey quitthen the output is:ereht olleH yeHThere is a mistake in my code I can not find. my output is : ereht olleH ereht olleHyeHHow can I fix this?I saw some people use void or the function reverse but we didnt...

  • Write a program in python that reads each line in a file, reverses its lines, and...

    Write a program in python that reads each line in a file, reverses its lines, and writes them to another file. For example, if the file input.txt contain the lines: Mary had a little lamb Its fleece was white as snow And everywhere that Mary went The lamb was sure to go. and you run your Python file then output.txt contains The lamb was sure to go. And everywhere that Mary went Its fleece was white as snow Mary had...

  • C++ (1) Write a program to prompt the user for an input and output file name....

    C++ (1) Write a program to prompt the user for an input and output file name. The program should check for errors in opening the files, and print the name of any file which has an error, and exit if an error occurs. For example, (user input shown in caps in first line, and in second case, trying to write to a folder which you may not have write authority in) Enter input filename: DOESNOTEXIST.T Error opening input file: DOESNOTEXIST.T...

  • Write a program that takes in a line of text as input, and outputs that line...

    Write a program that takes in a line of text as input, and outputs that line of text in reverse. You may assume that each line of text will not exceed 50 characters. The program repeats, ending when the user enters "Quit", "quit", or "q" for the line of text. Ex: If the input is: Hello there Hey quit then the output is: ereht olleH уен Hint: Use the fgets function to read a string with spaces from the user...

  • Write a program that reverses a string and prints it on the screen. Sample input/output: Enter...

    Write a program that reverses a string and prints it on the screen. Sample input/output: Enter a string to be reversed: Testing the reverse string program reversed string: margorp gnirts esrever eht gnitseT

  • After reading pages 330 - 336, write a program that takes two command line arguments which...

    After reading pages 330 - 336, write a program that takes two command line arguments which are file names. The program should read the first file line by line and write each line, in reverse order, into the second file. The program should include a "usage" method that displays the correct command line syntax if two file names are not provided. Example: if my input file says Hello, World! then my output file will contain !dlroW ,olleH Hints: Use CaesarCipher...

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