/* University College London Dept of P&A course in C++ 3C59 | all rights reserved 2000 | | Module: Consolidation excercise: | | Title: ComptonAnalysis | | Class: DataReader | | Description: | | This is an example of a class which performs some well defined service for | its client. In this case it is an "IO helper", meaning that its job is to help | the client with obtaining input to the program. | | In this simple example it actually only does one thing, which is to read a file | and use it to make a vector of Datum class objects. However it could equally well | also implement a method to get datums frome keyboard, or via an http://.. | connection to some remote web-server. In each case it would still returen a vector | of Datum objects to the client. | | To be clear, this is a pretty dumb example purely because it is kep to be about as | short as possible. However the message you are supposed to get from this is: | | classes can be written to provide a set all sorts of services. Classes are not | limited to merely representing some piece of data such as a measurement. | | Author: P.Clarke */ #ifndef _datareader_h #define _datareader_h 1 #include #include #include #include #include #include "Datum.h" //----------------------------------------------- // Here we start the class declaration as normal class DataReader { private: // In this dump class it doesnt really need anym private member variables, but // they would go here if it did. public: // -------------------------------------------- // Beginning of method // This method performs the service of returning to the caller a vector // of Datum objects. It does this by accessing a file on disk which contains // the numbers it needs to make each Datum. It will stick them all into a vector // of datum objects and return this as its return argument. // It starts with the method signature as normal. // In case this looks strange to you then read it like this (reading left to right) // (i) it will return a vector of Datum objects // (ii) it is called readFromFile // (iii) it takes one argument which is an object of the string class std::vector readFromFile( std::string filename ) { // Here is the code of the method // ................... // First make an (empty) vector which we will fill in this method std::vector tempStore ; // .................... // Open an the input file specified as the argument to the method for reading // (the .c_str() is a silly complication which was explained in class) std::ifstream inputFile( filename.c_str() ) ; if( ! inputFile ) { // Print an error message and return the empty vector std::cout << " Error - could not open input file " << filename << std::endl ; return tempStore ; } // ................... // Now go through the file, reading each line at a time while( inputFile ) { // Read three numbers from one line of the file float angle, energy, err ; inputFile >> angle >> energy >> err ; // Fix up line - explained in class if( ! inputFile ) break ; // Make a new Datum with them Datum tempDatum ; tempDatum.initialise( angle, energy, err ) ; // Stick it in the vector which was made at beginning of method tempStore.push_back( tempDatum ) ; } // At this point we have filled the vector called tempStore with lots of // Datum objects created from the file/ all that remains is to return // it to the caller return tempStore ; } // End of method }; // End of class #endif