/******************************************************************************************** example 6 - readint.cpp read into an array, then display them in either direction ********************************************************************************************/ #include <iostream> #include <string> #include <fstream> using namespace std; #include "../ex05/safearray.h" typedef safearray<string> SAFEARRAY; //prototypes void readFrom(string file, SAFEARRAY&); void showForward(const SAFEARRAY&); void showBackward(const SAFEARRAY&); //------- The MAIN point of entry ------- void main() { safearray<string> Array; string filename; cout << "What is the file to read: "; cin >> filename; readFrom(filename, Array); cout << "Total Elements Used: " << Array.count() << endl << "Out of a maximum of: " << Array.maxsize << endl; showForward(Array); showBackward(Array); } //------- Load a list of integers from a text file ------- void readFrom(string file, SAFEARRAY& list) { ifstream fin(file.c_str()); for (int i = 0; i < list.maxsize; i++) if(!(fin >> list[i])) { list.remove(i); break; } } //------- Display the list of integers in the order they are stored ------- void showForward(const SAFEARRAY& list) { cout << "Forward:" << endl; for (int i = 0; i < list.count(); i++) cout << list[i] << endl; cout << endl; } //------- Display the list of integers in the reverse of the order they are stored ------- void showBackward(const SAFEARRAY& list) { cout << "Backward:" << endl; for (int i = list.count(); i > 0; i--) cout << list[i-1] << endl; cout << endl; }