Answer to Question #272924 in C++ for Amit

Question #272924

Write to program to create a file. input user defined data entered from keyboard and write to the file. Read the contents of the file in reverse order and store to another file.

1
Expert's answer
2021-11-29T02:53:29-0500
#include<iostream>
#include<string>
#include <fstream>


using namespace std;


int main()
{
	int n;
	string fileName="integers.txt";
	cout<<"Enter number of integers: ";
	cin>>n;
	// write to the file.
	ofstream integersFile;
	integersFile.open (fileName);
	for(int i=0;i<n;i++){
		int num;
		cout<<"Enter number "<<(i+1)<<": ";
		cin>>num;
		integersFile << num<<"\n";
	} 
	integersFile.close();
	int numbers[100];
	int counter=0;


	ifstream integersFileRead(fileName);
	if(!integersFileRead){
		cout << "The file '"<<fileName<<"' does not exist.\n";
	}else{
		int number;
		while(!integersFileRead.eof()){
			integersFileRead>>numbers[counter];
			counter++;
		}
	}
	integersFileRead.close(); 
	ofstream integersReverseOrderFile;
	integersReverseOrderFile.open ("integersReverseOrder.txt");
	for(int i=counter-2;i>=0;i--){
		integersReverseOrderFile <<numbers[i]<<"\n";
	} 
	integersReverseOrderFile.close();


	cin>>n;
	return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS