Question

In: Computer Science

Write a C++ programm which reads an input file named 'input.txt' content of input.txt are not...

Write a C++ programm which

  • reads an input file named 'input.txt'
  • content of input.txt are not known to you
  • copies content of input file into an output file named 'output.txt'
  • contents of output.txt should be exactly the same as contents of input.txt
  • the user should be given a choice to select input and output file in any folder
  • remember to check that input file opened successfully

Please add comments next to the code, so I can learn from the solution. Also please use beginner coding, nothing too advance.

Solutions

Expert Solution

Below is the solution:


#include <iostream>
#include <fstream>
using namespace std;

int main()
{
   fstream fRead,fWrite; //create a file stream to read and write a file
   char fname1[20], fname2[20]; //variable of input and output file name
   string line; //variable to read file line
   cout<<"Enter source file name with extension (input.txt) : "; //ask to enter file name to read
   gets(fname1); //file name from keyboard
   fRead.open(fname1); //open file to read
   if(!fRead) //if file is not found to read
   {
       cout<<"Error!! in opening file!"; //print the error
       exit(1); //exit
   }
   cout<<"Enter output file name with extension (output.txt) : "; //ask to enter file name to write
   gets(fname2); //file name from keyboard
   fWrite.open(fname2); //open a file to write
   if(!fWrite) //if file not found
   {
       cout<<"Error!! in opening file!";
       fRead.close(); //close file opened
       exit(2); //exit
   }
  
   while (getline(fRead, line) ) //loop until end of the file line by line
    {
       fWrite<<line<<endl; //write each line to the file
   }
   cout<<"File copied successfully..!!"; //message to file content written
   fRead.close(); //close the file
   fWrite.close(); //close the file
   return 0;
}

sample output:

Enter source file name with extension (input.txt) : input.txt
Enter output file name with extension (output.txt) : output.txt
File copied successfully..!!

Enter source file name with extension (input.txt) : input1.txt
Error!! in opening file!

input.txt:

This is a first line.
This is another line.
1 2 3 4 5

output.txt:

This is a first line.
This is another line.
1 2 3 4 5


Related Solutions

Write a C++ pgm which 1.reads an input file named 'input.txt' 2.content of input.txt are not...
Write a C++ pgm which 1.reads an input file named 'input.txt' 2.content of input.txt are not known to you 3.copies content of input file into an output file named 'output.txt' 4.contents of output.txt should be exactly the same as contents of input.txt 5.the user should be given a choice to select input and output file in any folder 6.remember to check that input file opened successfully
Write a program in Java that reads an input text file (named: input.txt) that has several...
Write a program in Java that reads an input text file (named: input.txt) that has several lines of text and put those line in a data structure, make all lowercase letters to uppercase and all uppercase letters to lowercase and writes the new lines to a file (named: output.txt).
Write a program in c that reads the content from the file and stores each line...
Write a program in c that reads the content from the file and stores each line in an int array in heap(using dynamic memory allocation). For example, let the file has elements following (we do not know the size of files, it could be above 100,000 and contents of the file and make sure to convert file elements to int): 10067 26789 6789 3467
Write a program that reads from the external file input.txt, capitalizes all words that begin with...
Write a program that reads from the external file input.txt, capitalizes all words that begin with the letter "a," and then writes them to an external file output.txt (Note: Do not forget to copy the blanks. You may wish to use infile.get and outfile.put in your program.) Output: After the program generates output.txt, the code should display the contents of the file on the screen to verification.
Write a Python program called “exam.py”. The input file, “input.txt”, is given to you in the...
Write a Python program called “exam.py”. The input file, “input.txt”, is given to you in the Canvas exam instructions. Name your output file “output.txt”. Ensure the program consists of a main function and at least one other function. Ensure the program will read the input file, and will output the following information to the output file as well as printing it to the screen: output the text of the file to screen output the number of words in the file...
C++ 10.15: Character Analysis Write a program that reads the contents of a file named text.txt...
C++ 10.15: Character Analysis Write a program that reads the contents of a file named text.txt and determines the following: The number of uppercase letters in the file The number of lowercase letters in the file The number of digits in the file Prompts And Output Labels. There are no prompts-- nothing is read from standard in, just from the file text.txt. Each of the numbers calculated is displayed on a separate line on standard output, preceded by the following...
Write a program which reads an input file. It should assume that all values in the...
Write a program which reads an input file. It should assume that all values in the input file are integers written in decimal. Your program should read all integers from the file and print their sum, maximum value, minimum value, and average. Use the FileClient class here (from a previous reading) as an example. You'll need to create a file to be used as input to test your program, as well. Your program should work whether the integers are separated...
Write a C program that, given a file named Program_2.dat as input, determines and prints the...
Write a C program that, given a file named Program_2.dat as input, determines and prints the following information: The number of characters in the file. The number of uppercase letters in the file. The number of lowercase letters in the file. The number of words in the file. The number of lines in the file. Your program should assume that the input file, Program_2.dat, may contain any text whatsoever, and that text might be, or might not be, the excerpt...
Write a program named FinalExamProgram2 that reads numbers from a file (which you will create using...
Write a program named FinalExamProgram2 that reads numbers from a file (which you will create using Notepad) into a one-dimensional array and then analyzes the numbers as described below. Your program must use loops to read the numbers into the array and to analyze the contents of the array. The program’s main function should do the following:  Read eight floating-point numbers from the file named numbers.txt into a onedimensional array, displaying each number on the screen.  Pass the...
C++ Write a program that prompts for a file name and then reads the file to...
C++ Write a program that prompts for a file name and then reads the file to check for balanced curly braces, {; parentheses, (); and square brackets, []. Use a stack to store the most recent unmatched left symbol. The program should ignore any character that is not a parenthesis, curly brace, or square bracket. Note that proper nesting is required. For instance, [a(b]c) is invalid. Display the line number the error occurred on. These are a few of the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT