Objective of this assignment : Read values from a user specified file (input11.t
ID: 3554321 • Letter: O
Question
Objective of this assignment :
Read values from a user specified file (input11.txt).
Write the information to a user specified output file and to a screen.
Step 1.
Receive a path and a file name from a keyboard and open an input file.
If a program fails to open a file, return an error message and exit.
Step 2.
Receive a path and a file name from a keyboard and open an output file.
Step 3.
Read information (10 integer numbers) from the input file. Show the
numbers. Store them in an array.
Step 4.
Sort the stored numbers in increasing order.
Step 5.
Write the result to a file as well as to a screen.
Step 6.
Close both input and output files and exit the program.
input11.txt:
Output Example(has 89 instead of 96 for input file listed above):
Output file:
Explanation / Answer
#include<iostream>
#include<fstream>
#include<algorithm>
#include<vector>
#include<string>
using namespace std ;
int main()
{
cout << "Please enter the name of input the file you wish to open : " ;
string s ;
getline(cin,s);
vector<int> data(10) ;
string s1 ;
ifstream myfile(s.c_str());
if (myfile.is_open())
{
cout << "The input file is successfully opened " ;
cout << "Please enter the name of the output file you wish to open : " ;
string s ;
getline(cin,s);
ofstream output(s.c_str());
if( output.is_open())
{
cout << "The output file has successfully opened " ;
for(int i = 0 ; i < 10 ; i++ )
myfile >> data[i] ;
cout << " The numbers in input file : " ;
for(int i = 0 ; i < 10 ; i++ )
cout << data[i] << " " ;
sort(data.begin() , data.end() );
cout << " The numbers in SORTED order : " ;
for(int i = 0 ; i < 10 ; i++ )
cout << data[i] << " " , output << data[i] << " " ;
output.close();
}
else
cout << "OUTPUT file cannot be opened" << endl ;
myfile.close();
}
else
cout << "Input file cannot be opened" << endl ;
//system("pause");
return 0 ;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.