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.
Write a C program to run on unix to read a text file and print it...
Write a C program to run on unix to read a text file and print it to the display. It should count of the number of words in the file, find the number of occurrences of a substring, and take all the words in the string and sort them (ASCII order). You must use getopt to parse the command line. There is no user input while this program is running. Usage: mywords [-cs] [-f substring] filename • The -c flag...
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...
Your assignment is to write a C++ program to read a text file containing the information...
Your assignment is to write a C++ program to read a text file containing the information of the employees of a company, load them into memory and perform some basic human resources operations. This assignment will help you practice: multiple file programming, classes, public and private methods, dynamic memory allocation, constructors and destructors, singly linked list and files. Implementation This lab assignment gives you the opportunity to practice creating classes and using dynamic memory in one of the required classes....
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...
In C++, write a program that reads data from a text file. Include in this program...
In C++, write a program that reads data from a text file. Include in this program functions that calculate the mean and the standard deviation. Make sure that the only global variables are the actual data points, the mean, the standard deviation, and the number of data entered. All other variables must be local to the function. At the top of the program make sure you use functional prototypes instead of writing each function before the main function... ALL LINES...
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{    ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT