// Programmed by William O'Brien . Essentially what // I've programmed is a class that simplifies reading formatting input // from files. I've made a main generic Retrieve class, and two others // that are specializations for int and double. These should make this // file pretty applicable as is for development. // To use this you simply pass the file name, format string and pointer // to buffer to store the value. The type of the buffer is passed to the // class as a template parameter. For complete examples check the main // driver program. Hope this could be of some use. // Note: If you are going to use this I would definately recommend that // you seperate the declarations and specialization declarations in a // header file along with their respective definitions. I would have done // so but I wanted things to be simple. #include #include #include #include using namespace std; template class Retrieve { private: ifstream ifInput; tArgumentType *tExtraction; string sSearchString; string sCurrentFileName; public: Retrieve(void); Retrieve(const char *szFileName); Retrieve(const string &sFileName); Retrieve(const char *szFileName, const char *szReadString, tArgumentType *tAddress); Retrieve(const string &sFileName, const string &sReadString, tArgumentType *tAddress); ~Retrieve(void); void CloseFile(void); void ClearState(void); void OpenFile(const char *szFileName); void OpenFile(const string &sFileName); void Read(void); void ReadSameFile(void); void SearchString(const char *szReadString); void SearchString(const string &sReadString); void ExtractAddress(tArgumentType *tAddress); }; template Retrieve::Retrieve(void) {} template Retrieve::Retrieve(const char *szFileName) : sCurrentFileName(szFileName) { OpenFile(szFileName); } template Retrieve::Retrieve(const string &sFileName) : sCurrentFileName(sFileName) { OpenFile(sFileName); } template Retrieve::Retrieve(const char *szFileName, const char *szReadString, tArgumentType *tAddress) : sCurrentFileName(szFileName) { sSearchString = string(szReadString); OpenFile(szFileName); ExtractAddress(tAddress); } template Retrieve::Retrieve(const string &sFileName, const string &sReadString, tArgumentType *tAddress) : sCurrentFileName(sFileName) { sSearchString = sReadString; OpenFile(sFileName); ExtractAddress(tAddress); } template Retrieve::~Retrieve(void) { CloseFile(); } template void Retrieve::CloseFile(void) { ifInput.close(); } template void Retrieve::ClearState(void) { ifInput.clear(); } template void Retrieve::OpenFile(const char *szFileName) { sCurrentFileName = string(szFileName); if (ifInput.is_open()) { ifInput.close(); } ifInput.open(szFileName); } template void Retrieve::OpenFile(const string &sFileName) { sCurrentFileName = string(sFileName); if (ifInput.is_open()) { ifInput.close(); } ifInput.open(sFileName.c_str()); } template void Retrieve::ExtractAddress(tArgumentType *tAddress) { tExtraction = tAddress; } template void Retrieve::Read(void) { string sLineBuffer; string sExtraction; ClearState(); if (ifInput) { getline(ifInput, sLineBuffer); } string::size_type stPosition = sLineBuffer.find(sSearchString); while (ifInput && stPosition == string::npos) { getline(ifInput, sLineBuffer); stPosition = sLineBuffer.find(sSearchString); } if (ifInput && stPosition != string::npos) { stPosition += sSearchString.size(); } sExtraction = sLineBuffer.substr(stPosition, string::npos); *tExtraction = sExtraction; } template void Retrieve::ReadSameFile(void) { CloseFile(); OpenFile(sCurrentFileName); Read(); } template void Retrieve::SearchString(const char *szReadString) { sSearchString = string(szReadString); } template void Retrieve::SearchString(const string &sReadString) { sSearchString = string(sReadString); } template <> class Retrieve { private: ifstream ifInput; tArgumentType *tExtraction; string sSearchString; string sCurrentFileName; public: Retrieve(void); Retrieve(const char *szFileName); Retrieve(const string &sFileName); Retrieve(const char *szFileName, const char *szReadString, tArgumentType *tAddress); Retrieve(const string &sFileName, const string &sReadString, tArgumentType *tAddress); ~Retrieve(void); void CloseFile(void); void ClearState(void); void OpenFile(const char *szFileName); void OpenFile(const string &sFileName); void Read(void); void ReadSameFile(void); void SearchString(const char *szReadString); void SearchString(const string &sReadString); void ExtractAddress(tArgumentType *tAddress); }; template <> Retrieve::Retrieve(void) {} template <> Retrieve::Retrieve(const char *szFileName) : sCurrentFileName(szFileName) { OpenFile(szFileName); } template <> Retrieve::Retrieve(const string &sFileName) : sCurrentFileName(sFileName) { OpenFile(sFileName); } template <> Retrieve::Retrieve(const char *szFileName, const char *szReadString, tArgumentType *tAddress) : sCurrentFileName(szFileName) { sSearchString = string(szReadString); OpenFile(szFileName); ExtractAddress(tAddress); } template <> Retrieve::Retrieve(const string &sFileName, const string &sReadString, tArgumentType *tAddress) : sCurrentFileName(sFileName) { sSearchString = sReadString; OpenFile(sFileName); ExtractAddress(tAddress); } template <> Retrieve::~Retrieve(void) { CloseFile(); } template <> void Retrieve::CloseFile(void) { ifInput.close(); } template <> void Retrieve::ClearState(void) { ifInput.clear(); } template <> void Retrieve::OpenFile(const char *szFileName) { sCurrentFileName = string(szFileName); if (ifInput.is_open()) { ifInput.close(); } ifInput.open(szFileName); } template <> void Retrieve::OpenFile(const string &sFileName) { sCurrentFileName = string(sFileName); if (ifInput.is_open()) { ifInput.close(); } ifInput.open(sFileName.c_str()); } template <> void Retrieve::ExtractAddress(tArgumentType *tAddress) { tExtraction = tAddress; } template <> void Retrieve::Read(void) { string sLineBuffer; string sExtraction; ClearState(); if (ifInput) { getline(ifInput, sLineBuffer); } string::size_type stPosition = sLineBuffer.find(sSearchString); while (ifInput && stPosition == string::npos) { getline(ifInput, sLineBuffer); stPosition = sLineBuffer.find(sSearchString); } if (ifInput && stPosition != string::npos) { stPosition += sSearchString.size(); } sExtraction = sLineBuffer.substr(stPosition, string::npos); *tExtraction = atoi(sExtraction.c_str()); } template <> void Retrieve::ReadSameFile(void) { CloseFile(); OpenFile(sCurrentFileName); Read(); } template <> void Retrieve::SearchString(const char *szReadString) { sSearchString = string(szReadString); } template <> void Retrieve::SearchString(const string &sReadString) { sSearchString = string(sReadString); } template <> class Retrieve { private: ifstream ifInput; tArgumentType *tExtraction; string sSearchString; string sCurrentFileName; public: Retrieve(void); Retrieve(const char *szFileName); Retrieve(const string &sFileName); Retrieve(const char *szFileName, const char *szReadString, tArgumentType *tAddress); Retrieve(const string &sFileName, const string &sReadString, tArgumentType *tAddress); ~Retrieve(void); void CloseFile(void); void ClearState(void); void OpenFile(const char *szFileName); void OpenFile(const string &sFileName); void Read(void); void ReadSameFile(void); void SearchString(const char *szReadString); void SearchString(const string &sReadString); void ExtractAddress(tArgumentType *tAddress); }; template <> Retrieve::Retrieve(void) {} template <> Retrieve::Retrieve(const char *szFileName) : sCurrentFileName(szFileName) { OpenFile(szFileName); } template <> Retrieve::Retrieve(const string &sFileName) : sCurrentFileName(sFileName) { OpenFile(sFileName); } template <> Retrieve::Retrieve(const char *szFileName, const char *szReadString, tArgumentType *tAddress) : sCurrentFileName(szFileName) { sSearchString = string(szReadString); OpenFile(szFileName); ExtractAddress(tAddress); } template <> Retrieve::Retrieve(const string &sFileName, const string &sReadString, tArgumentType *tAddress) : sCurrentFileName(sFileName) { sSearchString = sReadString; OpenFile(sFileName); ExtractAddress(tAddress); } template <> Retrieve::~Retrieve(void) { CloseFile(); } template <> void Retrieve::CloseFile(void) { ifInput.close(); } template <> void Retrieve::ClearState(void) { ifInput.clear(); } template <> void Retrieve::OpenFile(const char *szFileName) { sCurrentFileName = string(szFileName); if (ifInput.is_open()) { ifInput.close(); } ifInput.open(szFileName); } template <> void Retrieve::OpenFile(const string &sFileName) { sCurrentFileName = string(sFileName); if (ifInput.is_open()) { ifInput.close(); } ifInput.open(sFileName.c_str()); } template <> void Retrieve::ExtractAddress(tArgumentType *tAddress) { tExtraction = tAddress; } template <> void Retrieve::Read(void) { string sLineBuffer; string sExtraction; ClearState(); if (ifInput) { getline(ifInput, sLineBuffer); } string::size_type stPosition = sLineBuffer.find(sSearchString); while (ifInput && stPosition == string::npos) { getline(ifInput, sLineBuffer); stPosition = sLineBuffer.find(sSearchString); } if (ifInput && stPosition != string::npos) { stPosition += sSearchString.size(); } sExtraction = sLineBuffer.substr(stPosition, string::npos); *tExtraction = atof(sExtraction.c_str()); } template <> void Retrieve::ReadSameFile(void) { CloseFile(); OpenFile(sCurrentFileName); Read(); } template <> void Retrieve::SearchString(const char *szReadString) { sSearchString = string(szReadString); } template <> void Retrieve::SearchString(const string &sReadString) { sSearchString = string(sReadString); } // Data.dat contents: // Programmer: William O'Brien // Age: 17 // Language: C, C++, STL // Dream: Game Programmer int main(void) { string sProgrammerAge; Retrieve rAge("Data.dat", "Age: ", &sProgrammerAge); rAge.Read(); cout << sProgrammerAge << endl; rAge.SearchString("Programmer: "); rAge.ReadSameFile(); cout << sProgrammerAge << endl; rAge.CloseFile(); int iProgrammerAge; Retrieve rAgeAgain("Data.dat", "Age: ", &iProgrammerAge); rAgeAgain.Read(); cout << iProgrammerAge << endl; return 0; }