C++
create a program that:
in main:
-opens the file provided for input (this file is titled 'Lab_HW10_Input.txt' and simply has 1-10, with each number on a new line for 10 lines total)
-calls a function to determine how many lines are in the file
-creates an array of the proper size
-calls a function to read the file and populate the array
-calls a function to write out the contents of the array in reverse order
*output file should be named 'reverse_output.txt'
Following is the required code and it's been written in Code::Blocks IDE:
The code BEGINS from here:
#include <iostream>
#include <fstream>
#include <stdlib.h>
using namespace std;
// SIGNATURES of the required functions
int lines (ifstream&);
void populate (ifstream&, int []);
void rvrsArrWrite (ofstream&, int [], int);
// MAIN FUNCTION
int main()
{
int filesize;
// Opening file streams to input and output files
ifstream inputFile("Lab_HW10_Input.txt");
ofstream outputFile("reverse_output.txt");
// If input file is not found
if(inputFile == NULL){
cout << "File Does Not Exist!";
exit(0);
}
// call to function lines() and assigning the returned value to
'filesize'
filesize = lines(inputFile);
cout << "Number of lines in the file: " << filesize
<< endl;
// To make the pointer go back to the beginning of the
file
inputFile.clear();
inputFile.seekg(0, ios::beg);
// creating an array of proper size
int arr[filesize];
// call to function populate()
populate(inputFile, arr);
// Elements in the array
cout << "Following are the elements in the array: "
<< endl;
for(int i = 0; i < filesize; i++){
cout << arr[i] << " ";
}
// call to the function rvrsArrWrite()
rvrsArrWrite(outputFile, arr, filesize);
// Closing the File Streams
inputFile.close();
outputFile.close();
cout << endl;
return 0;
}
// Function lines() - to determine the number of lines in the
file
// @param ifstream& - a reference to the file input stream
variable
int lines (ifstream &file){
string tname;
int len = 0; // stores the number of lines in the
file
while (std::getline(file, tname)) {
len++;
}
return len;
}
// Function populate() - to read the file and populate the
array
// @param ifstream& - a reference to the file instream
variable
void populate(ifstream &file, int arr[]){
int temp; // to temporarily store the data from
the file, for each line
int i = 0; //for indexing of array
while(file>>temp){
arr[i] = temp;
i++;
}
cout << "\nThe array has been populated Successfully!"
<< endl;
}
// Function rvrsArrWrite() - to write the contents of array to a
file in reverse order
// @param ofstream& - a reference to the file outstream
variable
// @param int [] - integer array to be reversed and written to
file
// @param int - size of the array
void rvrsArrWrite(ofstream &file, int arr[], int
length){
// Looping through each element in the array and writing it to
the file
for(int i = length-1; i >= 0; i--){
file << arr[i] << endl;
}
cout << "\n\nWriting to the file named \"reverse_output\" is
done Successfully!" << endl;
cout << "Please open the file in a text editor to check its
contents..." << endl;
}
The code ENDS here!
Some KeyPoints about the program:
int arr[ filesize ];
inputFile.clear();
inputFile.seekg(0, ios::beg);
These statements are very necessary inn this program. Allow me to explain why? After using the funtion, lines(), we read the file and as the pointer finishes reading the last data in the file, it sets the eof (End of File). Then if we hadn't used the above statements and try to use the file again in populate() function, it would have read nothing, because the pointer is already at the end. So these two statements clear the eof flag and sends the pointer back to the starting of the file.
Following is a ScreenShot of the sample data in the 'Lab_HW10_Input.txt' file:
This is the Screenshot of the Output for a Test Run for above input file:
And here is the ScreenShot of the "reverse_output.txt" file after the program executed:
Hope the code meets all your requirements.
Best Wishes!
Get Answers For Free
Most questions answered within 1 hours.