Question

In: Computer Science

HW_6b - Read and write a text file. Include the following in the main.cpp file. Add...

HW_6b - Read and write a text file.

  1. Include the following in the main.cpp file.

  1. Add code so that the numbers that are read from the data.txt file are written to an output

          file named:   results.txt

  1. After the numbers are written to the file, the following message should be displayed:

   The data has been written to the file.

  1. Open the results.txt file and verify that the file was written successfully.

Please make the code based on C++, and write a description for each code.

Solutions

Expert Solution

Here is the answer for your question in C++ Programming Language.

Kindly upvote if you find the answer helpful.

##############################################################

CODE :

#include<iostream>
#include<fstream>

using namespace std;

int main(){
   //Create input file object
   ifstream inFile;
   //Open input file data.txt
   inFile.open("data.txt");
  
   //Create output file object
   ofstream outFile;
   //Create and Open output file "results.txt"
   outFile.open("results.txt");
  
   //Required variable
   int n;
  
   //If the file is not found
   if(!inFile){
       //Print appropriate message and exit
       cout << "File not found" << endl;
       return 1;
   }else{
       //Otherwise
       //Read each number in the input file and store it in 'n'
       while(inFile >> n) {  
           //Write 'n' to output file,one 'n' per line
           outFile << n << endl;
       }
       //Close both the file objects
       inFile.close();
       outFile.close();
       //Print message
       cout << "The data has been written to the file." << endl;
   }
   //To check whether data is successfully written
   //Create input file object
   ifstream readFile;
   //Open the file results.txt
   readFile.open("results.txt");
  
   //Read and print data from results.txt file
   cout << endl << "Data from results.txt file: " << endl;
   cout << "=================================" << endl;
   //Read each number from results.txt file and store it in 'n'
   while(readFile >> n) {      
       //Display 'n' to console
       cout << n << endl;
   }
   //Close the file object
   readFile.close();
      
   return 0;
}

################################

data.txt

102
103
105
106 109 110
112 134 156

#################################################################

SCREENSHOTS :

Please see the screenshots of the code below for the indentations of the code.

###########################################

data.txt

#####################################################################

OUTPUT :

results.txt file

CONSOLE OUTPUT :

Any doubts regarding this can be explained with pleasure :)


Related Solutions

HW_6c - Append data to existing data in a file. Include the following in the main.cpp...
HW_6c - Append data to existing data in a file. Include the following in the main.cpp file. Prompt the user to enter 3 more numbers, then read the numbers and write them to             the results.txt file. Include code so that when the data is written to file, it is appended (added on to the     end of the existing data). Open the results.txt file and verify that the file was written successfully. /* OUTPUT Here are the numbers...
Please write a java program to write to a text file and to read from a...
Please write a java program to write to a text file and to read from a text file.
I need to write a java program (in eclipse) that will read my text file and...
I need to write a java program (in eclipse) that will read my text file and replace specific placeholders with information provided in a second text file. For this assignment I am given a text file and I must replace <N>, <A>, <G>, with the information in the second file. For example the information can be John 22 male, and the template will then be modified and saved into a new file or files (because there will be multiple entries...
Create a CodeBlocks project with a main.cpp file. Submit the main.cpp file in Canvas. C++ Language...
Create a CodeBlocks project with a main.cpp file. Submit the main.cpp file in Canvas. C++ Language A Game store sells many types of gaming consoles. The console brands are Xbox, Nintendo, PlayStation. A console can have either 16 or 8 gigabytes of memory. Use can choose the shipping method as either Regular (Cost it $5) or Expedite (Cost is $10) The price list is given as follows: Memory size/Brand Xbox Nintendo PlayStation 16 gigabytes 499.99 469.99 409.99 8 gigabytes 419.99...
Write a C++ program to create a text file. Your file should contain the following text:...
Write a C++ program to create a text file. Your file should contain the following text: Batch files are text files created by programmer. The file is written in notepad. Creating a text file and writing to it by using fstream: to write to a file, you need to open thew file as write mode. To do so, include a header filr to your program. Create an object of type fsrteam. Open the file as write mode. Reading from a...
please use text only! Instructions Consider the provided C++ code in the main.cpp file: The function...
please use text only! Instructions Consider the provided C++ code in the main.cpp file: The function func2 has three parameters of type int, int, and double, say a, b, and c, respectively. Write the definition of func2 so that its action is as follows: Prompt the user to input two integers and store the numbers in a and b, respectively. If both of the numbers are nonzero: If a >= b, the value assigned to c is a to the...
C++ Parse text (with delimiter) read from file. If a Text file has input formatted such...
C++ Parse text (with delimiter) read from file. If a Text file has input formatted such as: "name,23" on every line where a comma is the delimiter, how would I separate the string "name" and integer "23" and set them to separate variables for every input of the text file? I am trying to set a name and age to an object and insert it into a vector. #include <iostream> #include <vector> #include <fstream> using namespace std; class Student{    ...
C++ exercise: The statements in the file main.cpp are in incorrect order. using namespace std; #include...
C++ exercise: The statements in the file main.cpp are in incorrect order. using namespace std; #include <iostream> int main() { string shape; double height; #include <string>    cout << "Enter the shape type: (rectangle, circle, cylinder) "; cin >> shape; cout << endl;    if (shape == "rectangle") { cout << "Area of the circle = " << PI * pow(radius, 2.0) << endl;    cout << "Circumference of the circle: " << 2 * PI * radius << endl;...
how to read, write and append a text file using a switch case in java.I need...
how to read, write and append a text file using a switch case in java.I need help with the code.
How do I do this: Write a program that can read a text file of numbers...
How do I do this: Write a program that can read a text file of numbers and calculate the mean and standard deviation of those numbers. Print the result in another text file. Put the result on the computer screen. EACH LINE OF THE PROGRAM MUST BE COMMENTED!
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT