Ini File Manager
Hi guys,
I was doing my business when I thought that release this little class could be useful for someone out there. Here you go (done in VS2012):
IniFile.h :
IniFile.cpp :
Usage :
Screenshot:
I was doing my business when I thought that release this little class could be useful for someone out there. Here you go (done in VS2012):
IniFile.h :
Code:
#include "stdafx.h"
#include <windows.h>
class IniFile{
public:
IniFile(const char*); //constructor
char* ReadValue(const char*,const char*); //general func to read the value
bool WriteString(const char*,const char*,const char*); //func to write a string
bool WriteInteger(const char*,const char*,int); //func to write an integer
bool WriteBool(const char*,const char*,bool); //func to write a boolean
bool WriteDouble(const char*, const char*, double); //func to write a double
private:
bool WriteiniString(const char*,const char*,const char*); //private func used to write all the previous types
~IniFile(); //deconstructor
const char* dir; //path of the file (assigned only once in the constructor)
};
Code:
#include "stdafx.h"
#include "IniFile.h"
IniFile::IniFile(const char* uri){
dir = uri; //assign the path of the file
}
IniFile::~IniFile(){
delete(dir); //free the memory of the pointer on deconstruction
}
bool IniFile::WriteString(const char* section, const char* key, const char* value){
return WriteiniString(section, key, value);
}
bool IniFile::WriteInteger(const char* section, const char* key, int value){
char* buffer = new char[7]; //buffer for the conversion from int to char*
_itoa(value,buffer,10); //actually, the conversion
return WriteiniString(section, key, buffer);
}
bool IniFile::WriteDouble(const char* section, const char* key, double value){
const int numOfDigits = 6, bufferLength = 32; //1st is n° of digits, 2nd is the buffer length
char* buffer = new char[bufferLength];
_gcvt(value,numOfDigits,buffer); //the conversion
return WriteiniString(section, key, buffer);
}
bool IniFile::WriteBool(const char* section, const char* key, bool value){
return value? WriteiniString(section, key, "true") : WriteiniString(section, key, "false");
}
bool IniFile::WriteiniString(const char* section, const char* key, const char* value){
return WritePrivateProfileStringA(section, key, value, dir);
}
char* IniFile::ReadValue(const char* key, const char* variable){
const int length = 100; //max readable chars (you can edit the length)
char* buffer = new char[length]; //buffer for the reading of the value
GetPrivateProfileStringA(key,variable,NULL,buffer,length,dir);
return buffer;
}
Code:
#include "stdafx.h"
#include "IniFile.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
IniFile * file = new IniFile("D:\\test.ini"); //create the file
file->WriteString("TEST", "String", "string"); //write some values
file->WriteBool("TEST", "Bool", false);
file->WriteInteger("TEST", "Int", 1234);
file->WriteDouble("TEST", "Double", 56.78);
cout<<file->ReadValue("TEST","String")<<endl; //read and print them
cout<<file->ReadValue("TEST","Bool")<<endl;
cout<<file->ReadValue("TEST","Int")<<endl;
cout<<file->ReadValue("TEST","Double")<<endl;
return 0;
}
Screenshot:



