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.
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...
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...
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...
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!
Python program: Write a program that reads a text file named test_scores.txt to read the name...
Python program: Write a program that reads a text file named test_scores.txt to read the name of the student and his/her scores for 3 tests. The program should display class average for first test (average of scores of test 1) and average (average of 3 tests) for each student. Expected Output: ['John', '25', '26', '27'] ['Michael', '24', '28', '29'] ['Adelle', '23', '24', '20'] [['John', '25', '26', '27'], ['Michael', '24', '28', '29'], ['Adelle', '23', '24', '20']] Class average for test 1...
Write a C++ program to open and read a text file and count each unique token...
Write a C++ program to open and read a text file and count each unique token (word) by creating a new data type, struct, and by managing a vector of struct objects, passing the vector into and out of a function. Declare a struct TokenFreq that consists of two data members: (1) string value; and (2) int freq; Obviously, an object of this struct will be used to store a specific token and its frequency. For example, the following object...
Consider the following header file for the intArray object and the following main.cpp and answer the...
Consider the following header file for the intArray object and the following main.cpp and answer the following questions: intArray.h main.cpp #include <iostream> using namespace std; class intArray { friend ostream& operator<<(ostream& outStream, intArray& rhs); public:     intArray();     intArray(int _size);     intArray(int _size, int array[]);     intArray(const intArray&);     ~intArray();     void Print();     void PrintSize();     int Size() const;     int operator[](int) const;     intArray operator=(const intArray ); private:     int size;     int *data; }; 7 #include <iostream>...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT