More Advanced, maybe not Class problem binary file mode

Started by
10 comments, last by justinwalsh 20 years, 5 months ago
Well, I took all of your advice and made a config file class, thats a tad more generic, not too advanced but gets the job done forwhat i want it to do. Hear is the code...

ConfigWriter.h
#ifndef CONFIGWRITER_H#define CONFIGWRITER_H//////////////////////////////////////////////////////////		ConfigWriter.h//		Justin Walsh 11/17/2003//		ConfigWriter  - a class to handle writing and//			reading a basic config file, stores values//			as labels, followed by an int.///////////////////////////////////////////////////////						#include <iostream>#include <fstream>#include <vector>#include <string>using namespace std;#define cfg_nomatch 9999struct field {	int value;	string label;};class ConfigWriter  {	private:		vector fileEntries;		char* filename;		ofstream fout;		ifstream fin;			public:		ConfigWriter(char* filename);		~ConfigWriter();		void AddEntry(string eLabel, int eValue);		int GetEntry(string searchVal);		bool write(void);		bool read(void);};#endif 



ConfigWriter.cpp
   #include "ConfigWriter.h"ConfigWriter::ConfigWriter(char *filename)  {	ConfigWriter::filename = filename;}ConfigWriter::~ConfigWriter()  {	fileEntries.clear();}bool ConfigWriter::write()  {	fout.open(filename);	if(!fout.good())	//if not good rtrn false		return false;	for(int i=0; i < fileEntries.size(); i++)  {		fout << fileEntries.label << " " << fileEntries.value << endl;<br>	}<br><br>	fout << flush;<br>	fout.close();<br><br>	return true;<br>}<br><br>bool ConfigWriter::read()  {<br>	fin.open(filename);<br>	field temp;<br><br>	if(!fin.good())<br>		return false;<br><br>	while(!fin.eof()) {<br>		fin >> temp.label;<br>		fin >> temp.value;<br>		fileEntries.push_back(temp);<br>	}<br>	fin.close();<br>	return true;<br>}<br><br>void ConfigWriter::AddEntry(string eLabel, int eValue)  {<br>	field temp;<br>	temp.label = eLabel;<br>	temp.value = eValue;<br>	fileEntries.push_back(temp);<br>}<br><br>int ConfigWriter::GetEntry(string searchVal)  {<br>	for(int i=0; i < fileEntries.size(); i++)  {<br>		if(fileEntries.label == searchVal)<br>			return fileEntries.value;<br>	}<br>	return cfg_nomatch;<br>}<br> </pre> <br><br>I know it isnt very advanced,  but again gets th job done, has readable text file.  Will probably write another class over this &#111;ne that will actually do the settings of the window etc, but give some replies as to this way of doing things.  </i>  <br><br><SPAN CLASS=editedby>[edited by - justinwalsh on November 17, 2003 2:29:19 AM]</SPAN>
Advertisement
Try using std::numeric_limits<int>::max() instead of nomatch.

[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement