Ini File Class
-
Kleine Hilfe für Ini Files
wFileClass:
/********************************\ |* Author : WoH-xD *| |* Time : 13:22 *| |* Date : 11.09.2007 *| \********************************/ #ifndef _FILECLASS_H #define _FILECLASS_H #include <windows.h> #include <stdio.h> #include <stdlib.h> class wIniFile { public: VOID SetFilePath(LPSTR lpPath); BOOL FileExists(); INT ReadInt(LPSTR lpSection,LPSTR lpKey,LPSTR lpDefault); LPSTR ReadString(LPSTR lpSection,LPSTR lpKey,LPSTR lpDefault); BOOL WriteInt(LPSTR lpSection,LPSTR lpKey,INT iValue); BOOL WriteString(LPSTR lpSection,LPSTR lpKey,LPSTR lpValue); BOOL WriteSection(LPSTR lpSection,LPSTR lpValue); private: LPSTR lpFilePath; }; /******************************************************************** SetFilePath: lpPath = Pfad zur Datei Beispiel: wIniFile File; CHAR szPath[MAX_PATH] = ""; GetCurrentDirectoryA(MAX_PATH,szPath); strcat(szPath,"\\Beispiel.ini"); File.SetFilePath(szPath); ... ********************************************************************/ VOID wIniFile::SetFilePath(LPSTR lpPath) { lpFilePath = lpPath; } BOOL wIniFile::FileExists() { BOOL bReturn = FALSE; FILE *fFile; fFile = fopen(lpFilePath,"r"); if(fFile) bReturn = TRUE; return bReturn; } /******************************************************************** Read(String/Int): lpSection = Section Name lpKey = Key Name lpDefault = Dieser Wert wird zurückgegeben wenn der key nicht existiert oder kein Wert angegeben ist. Return = String/Int IniDatei: [Beispiel] Key1=1024 Key2=Muster Text Beispiel: File.ReadInt("Beispiel","Key1",0); -> Return: 1024 File.ReadString("Beispiel","Key2",0); -> Return: Muster Text ********************************************************************/ INT wIniFile::ReadInt(LPSTR lpSection,LPSTR lpKey,LPSTR lpDefault) { INT iReturn = FALSE; CHAR szBuffer[8192] = ""; if(FileExists()) { GetPrivateProfileStringA(lpSection,lpKey,lpDefault,szBuffer,8192,lpFilePath); iReturn = atoi(szBuffer); } return iReturn; } LPSTR wIniFile::ReadString(LPSTR lpSection,LPSTR lpKey,LPSTR lpDefault) { CHAR szReturn[8192] = ""; if(FileExists()) { GetPrivateProfileStringA(lpSection,lpKey,lpDefault,szReturn,8192,lpFilePath); } return szReturn; } /******************************************************************** Write(String/Int): lpSection = Section Name lpKey = Key Name (String/Int) lpValue = String iValue = INT Return = TRUE/FALSE Beispiel: File.WriteString("Beispiel","Key1","Hallo"); File.WriteInt("Beispiel","Key2",500); IniFile: [Beispiel] Key1=Hallo Key2=500 ********************************************************************/ BOOL wIniFile::WriteInt(LPSTR lpSection,LPSTR lpKey,INT iValue) { BOOL bReturn = FALSE; CHAR szBuffer[8192] = ""; if(FileExists()) { sprintf(szBuffer,"%d",iValue); if(WritePrivateProfileStringA(lpSection,lpKey,szBuffer,lpFilePath)) { bReturn = TRUE; } } return bReturn; } BOOL wIniFile::WriteString(LPSTR lpSection,LPSTR lpKey,LPSTR lpValue) { BOOL bReturn = FALSE; if(FileExists()) { if(WritePrivateProfileStringA(lpSection,lpKey,lpValue,lpFilePath)) { bReturn = TRUE; } } return bReturn; } /******************************************************************** WriteSection: lpSection = Section Name lpValue = Einträge (getrennt mit \0) Return = TRUE/FALSE Beispiel: File.WriteSection("Beispiel","Key1=Max\0Key2=Mustermann"); IniFile: [Beispiel] Key1=Max Key2=Mustermann ********************************************************************/ BOOL wIniFile::WriteSection(LPSTR lpSection,LPSTR lpValue) { BOOL bReturn = FALSE; if(FileExists()) { if(WritePrivateProfileSectionA(lpSection,lpValue,lpFilePath)) { bReturn = TRUE; } } return bReturn; } #endifBeispiel zur Anwendung:
CHAR szPath[MAX_PATH] = ""; wIniFile File; GetCurrentDirectoryA(MAX_PATH,szPath); strcat(szPath,"\\Beispiel.txt"); File.SetFilePath(szPath); if(File.FileExists()) { File.WriteString("Beispiel","Key1","Max Mustermann"); cout << File.ReadString("Beispiel","Key1",0); }
-
ich sehe ein fopen aber kein fclose?
und VisualC++ Express sicherlich auch die anderen VC++ Versionen mit aktuellem SDK werden beim fopen und sprintf meckern das sie als alt deklariert wurde und man fopen_s() bzw sprinf_s verwenden sollte.
-
ich habe das mit fclose vergessen ^ xd
aber ich bin fopen gewöhnt benutze VS 2005
ja stimmt schon fopen_s sollte man nehmen aber ist ansichts sache xD
-
Und was ist jetzt das Problem?
-
tenchou schrieb:
Und was ist jetzt das Problem?
Es gibt kein 's. Er präsentiert uns wohl einfach nur seine Arbeit

-
Tjo und die ist miserabel:
#if !defined(INI_FILE_H__INCLUDED) #define INI_FILE_H__INCLUDED #if (_MSC_VER >= 1300) #pragma once #endif // (_MSC_VER >= 1300) #include <windows.h> #include <cstdio> #include <string> #include <sstream> class IniFile { public: IniFile() : m_lpFilePath(NULL) {} IniFile(const char* const path) { set_file_path(path); } ~IniFile() { delete [] m_lpFilePath; } public: void set_file_path(const char* const path) { if (path != NULL) { delete [] m_lpFilePath; std::size_t len = std::strlen(path); m_lpFilePath = new char[len + 1]; std::strncpy(m_lpFilePath, path, len); if (file_exists() == false) throw std::runtime_error("invalid file path"); } } template<typename T> T read_value(const char* const lpSection, const char* const lpKey, const char* const lpDefault = "") const { if (lpSection == NULL || lpKey == NULL) throw std::invalid_argument(""); if (file_exists() == false) throw std::runtime_error("file does not exist"); char buf[2048]; if (::GetPrivateProfileStringA(lpSection, lpKey, lpDefault, buf, 2047, m_lpFilePath) <= 0) throw std::runtime_error("data do not exist"); std::istringstream ss(buf); T tmp; ss >> tmp; if (!ss) throw std::runtime_error("invalid data type"); return tmp; } template<typename T> void write_value(const char* const lpSection, const char* const lpKey, T const& value) const { if (lpSection == NULL || lpKey == NULL) throw std::invalid_argument(""); if (file_exists() == false) throw std::runtime_error("file does not exist"); std::ostringstream ss; ss << value; if (!ss) throw std::runtime_error("invalid data type"); if (::WritePrivateProfileStringA(lpSection, lpKey, ss.str().c_str(), m_lpFilePath) == FALSE) throw std::runtime_error("could not write data"); } void write_section(const char* const lpSection, const char* const lpValue) const { if (lpSection == NULL || lpKey == NULL) throw std::invalid_argument(""); if (file_exists() == false) throw std::runtime_error("file does not exist"); if (::WritePrivateProfileSectionA(lpSection, lpValue, m_lpFilePath) == FALSE) throw std::runtime_error("could not write data"); } private: bool file_exists() const { if (m_lpFilePath == NULL) return false; std::FILE* data = std::fopen(m_lpFilename, "r"); if (!data) return false; std::fclose(data); return true; } private: char* m_lpFilePath; }; #endif // INI_FILE_H__INCLUDED...
#include "inifile.h" #include <iostream> int main() { try { IniFile file("test.ini"); std::cout << file.read_value<unsigned int>("Beispiel", "Key A") << std::endl; } catch (std::exception& ex) { std::cerr << ex.what() << std::endl; } }
}
-
(D)Evil schrieb:
Tjo und die ist miserabel
Sowas finde ich unter aller Sau

-
Badestrand schrieb:
(D)Evil schrieb:
Tjo und die ist miserabel
Sowas finde ich unter aller Sau

Er meint doch seinen eigenen Kot.

-
Tja ist ja schön wenn du das so empfindest. Ok "miserabel" war etwas hart ausgedrückt ... jedenfalls beachtet er da einige Grundlagen nicht. Wenn er hier schon seine Klasse (fast) kommentarlos reinklatscht, sollte die wenigstens einigermaßen fehlerfrei sein.
-
Och ich find es schön, wenn man lokale variablen zurückgibt, das hat was von "hier kriegst du was, aber bitte benutze es nicht"
-
Hast schon recht, ich hatte einen schlechten Tag, tut mir Leid. Trotzdem, so eine Aussage wie "das ist miserabel" hätte man auch freundlicher gestalten können, z.B. auf Fehler hinweisen oder so
Nix für ungut!
-
GetCurrentDirectoryA würde ich in dem Zusammenhang auch nicht verwenden.
Aber harte Worte hier.....nicht nett. Aber sind ja immer die gleichen
die sowas sagen
-
Mir ist schlecht. Man bringe mir einen Küüübel