Question

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 Enter input filename: SOURCE.CPP Enter output filename: .. Error opening output file: .. (2) It should copy the input file, line by line, to the output file and to standard output so you can see it working (3) Before the program exits, it should close all files. (4) You will have to manually look at the output file to confirm the program ran correctly. A sample input file is listed below for your testing, or you may use source.cpp as your input to see your code copied to the output file. If you use our sample file, copy/paste the text into a new .txt file in the same folder as your source.cpp file, or your program won't be able to find it. For example, (based on the input file we provide you, with user input shown in caps) Enter input filename: FB.TXT Enter output filename: O.TXT File text: Copy this text file, exactly, line by line, without losing any words or whitespace. Sample input file "FB.TXT" which generated the above output: Copy this text file, exactly, line by line, without losing any words or whitespace.

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

#include<iostream>
#include<conio.h>
#include<fstream>
using namespace std;
/*The C++ Program which reads the file content line by line
and copying into another file*/
int main()
{
   /*The varibales used are declaring below
   inputstearm stream object to read the file content
   outputstream is the stream object to write the output file
   ch is an character array to store the line read from input file and
   writing into output file
   input is an char array to store the input file name
   output is the char array to storw the output file name
   */
   ifstream inputstream;
   ofstream outputstream;
   char ch[70], input[20], output[20];
   //Reading input file name
   cout<<"\nEnter input filename: ";
   gets(input);
   //File is is opening to read
   inputstream.open(input);
   //Checking for is the file opened successfully
   if(!inputstream)
   {//The file is not opened successfully
   //Displaying the error msg to user and terminating the program
       cout<<"\nError opening input file: DOESNOTEXIST.";
       getch();
       exit(1);
   }
   //Reading the output file name from user
   cout<<"\nEnter output filename: ";
   gets(output);
   //File opening to write the content of input file
   outputstream.open(output);
   //   //Checking for is the file opened successfully
   if(!outputstream)
   {//The file is not opened successfully
   //Displaying the error msg to user and terminating the program
       cout<<"\nError opening output file: .";
       //the input file stream is closing since error file opeing the output file stream
       inputstream.close();
       getch();
       exit(2);
   }
   //Both the files opens successfully
   //hence copying the content of input file to output file
   //Looping condition checking for eof file
   //loop iterate until the end of file reached
   //reading line by line and writing it into
   //output file
   while(inputstream.eof()==0)
   {
       inputstream.get(ch,70,'.');
       outputstream<<ch;
   }
   //After reading the file all stream object is closing
   cout<<"File copied successfully..!!";
   inputstream.close();
   outputstream.close();
   getch();
}

input file

in.txt

Add a comment
Know the answer?
Add Answer to:
C++ (1) Write a program to prompt the user for an input and output file name....
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
  • Please write this in C. Write this code in Visual Studio and upload your Source.cpp file for checking (1) Write a program to prompt the user for an output file name and 2 input file names. The progra...

    Please write this in C. Write this code in Visual Studio and upload your Source.cpp file for checking (1) Write a program to prompt the user for an output file name and 2 input file names. 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 opening any of the 3 For example, (user input shown in caps in first line) Enter first...

  • Write a program that asks the user for the name of an image, the name of an output file. Your pro...

    Write a program that asks the user for the name of an image, the name of an output file. Your program should then save the upper right quarter of the image to the output file specified by the user. A sample run of your program should look like: Enter image file name: hunterLogo.png Enter output file: logOUR.png which would have as input and output: HUNTER ITER The City University of New York Hint: See sample programs from Lectures 3 and...

  • C++ please! (1) Prompt the user to enter the name of the input file. The file...

    C++ please! (1) Prompt the user to enter the name of the input file. The file will contain a text on a single line. Store the text in a string. Output the string. Ex: Enter file name: input1.txt File content: We'll continue our quest in space. There will be more shuttle flights and more shuttle crews and, yes, more volunteers, more civilians, more teachers in space. Nothing ends here; our hopes and our journeys continue! (2) Implement a PrintMenu() function,...

  • Write a PYTHON program that asks the user for the name of the file. The program...

    Write a PYTHON program that asks the user for the name of the file. The program should write the contents of this input file to an output file. In the output file, each line should be preceded with a line number followed by a colon. The output file will have the same name as the input filename, preceded by “ln” (for linenumbers). Be sure to use Try/except to catch all exceptions. For example, when prompted, if the user specifies “sampleprogram.py”...

  • JAVA Write a program that prompts the user to enter a file name, then opens the...

    JAVA Write a program that prompts the user to enter a file name, then opens the file in text mode and reads it. The input files are assumed to be in CSV format. The input files contain a list of integers on each line separated by commas. The program should read each line, sort the numbers and print the comma separated list of integers on the console. Each sorted list of integers from the same line should be printed together...

  • Write a complete Python program with prompts for the user for the main text file (checks...

    Write a complete Python program with prompts for the user for the main text file (checks that it exists, and if not, output an error message and stop), for any possible flags (including none), and for any other input that this program may need from the user: split has an option of naming the smaller files head_tail list the first 10 lines (default) and the last 10 lines (default) in order of the given text file flag: -# output #...

  • 12.8 GPA reports using files Prompt the user by "Enter name of input file: " for...

    12.8 GPA reports using files Prompt the user by "Enter name of input file: " for the name of an input file and open that file for reading. The two input files for the tests are already at the zyBooks site.They each have a list of student names with the course they have taken, of the form Jacobs, Anthony ECS10 4 B- ECS20 4 C+ ANS17 3 A ANS49H 2 D Wilson, Elaine ECS10 4 B+ ECS20 4 C+ ANS17...

  • Your program must prompt the user for the name of two files. The first is the...

    Your program must prompt the user for the name of two files. The first is the input, the second one is the output. It is assumed that the first file contains plain text, like a literary passage that you like. On output, the second file must contain exactly the passage provided in the first, but all in UPPERCASE. (I'm using Java)

  • A. File I/O using C library functions File I/O in C is achieved using a file...

    A. File I/O using C library functions File I/O in C is achieved using a file pointer to access or modify files. Processing files in C is a four-step process: o Declare a file pointer. o Open the desired file using the pointer. o Read from or write to the file and finally, o Close the file. FILE is a structure defined in <stdio.h>. Files can be opened using the fopen() function. This function takes two arguments, the filename and...

  • Write a program in C++ that prompts the user to input the name of a text...

    Write a program in C++ that prompts the user to input the name of a text file and then outputs the number of words in the file. You can consider a

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