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
:)