Гайд Как создать систему сохранение настроек

vk.com/ahkcsgocheat
Пользователь
Статус
Оффлайн
Регистрация
21 Апр 2020
Сообщения
381
Реакции[?]
64
Поинты[?]
2K
Не нашел гайдов, как сохранять настройки для программы. Написал этот:
Common.h:
C++:
/// Common.h
// Defines common used libraries/headers across project files
#pragma once

#include <Windows.h>
#include <TlHelp32.h>
#include <iostream>
#include <thread>
#include <string>
#include <cstdio>
#include <memory>
#include <vector>
#include <sys\stat.h>
// Add here headers

// Quick macros :+)
#define FATAL_ERROR(ERROR) (MessageBox(NULL, ERROR, "FATAL ERROR", MB_ICONSTOP|MB_OK))
#define SEPARATOR() (printf("$---------------------------------$\n"))
#define COLOR(COL) (SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), COL))

// Quick funcs :+)
void ClearScreen()
{
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    HANDLE hStdOut;
    DWORD count;
    DWORD cellCount;
    COORD homeCoords = { 0, 0 };

    hStdOut = GetStdHandle( STD_OUTPUT_HANDLE );
    if( hStdOut == INVALID_HANDLE_VALUE ) return;

    if(!GetConsoleScreenBufferInfo( hStdOut, &csbi ) ) return;
    cellCount = csbi.dwSize.X *csbi.dwSize.Y;

    if( !FillConsoleOutputCharacter( hStdOut, ( TCHAR ) ' ', cellCount, homeCoords, &count ) )
        return;

    if( !FillConsoleOutputAttribute( hStdOut, csbi.wAttributes, cellCount, homeCoords, &count ) )
        return;
    SetConsoleCursorPosition( hStdOut, homeCoords );
}
std::string ExePath()
{
    char buffer[MAX_PATH];
    GetModuleFileName( NULL, buffer, MAX_PATH );
    std::string::size_type pos = std::string( buffer ).find_last_of( "\\/" );
    return std::string( buffer ).substr( 0, pos);
}
void HideCursor()
{
    CONSOLE_CURSOR_INFO CurInfo = { 1, false };
    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &CurInfo);
}
bool FileExists(std::string Path)
{
    struct stat buffer;
    return (stat (Path.c_str(), &buffer) == 0);
}

// Quick enums :+)
enum ConCol
{
    black = 0,
    dark_blue = 1,
    dark_green = 2,
    dark_aqua, dark_cyan = 3,
    dark_red = 4,
    dark_purple = 5, dark_pink = 5, dark_magenta = 5,
    dark_yellow = 6,
    dark_white = 7,
    gray = 8,
    blue = 9,
    green = 10,
    aqua = 11, cyan = 11,
    red = 12,
    purple = 13, pink = 13, magenta = 13,
    yellow = 14,
    white = 15
};
enum WeaponIDI
{
    WEAPON_DEAGLE = 1,
    WEAPON_ELITE = 2,
    WEAPON_FIVESEVEN = 3,
    WEAPON_GLOCK = 4,
    WEAPON_AK47 = 7,
    WEAPON_AUG = 8,
    WEAPON_AWP = 9,
    WEAPON_FAMAS = 10,
    WEAPON_G3SG1 = 11,
    WEAPON_GALILAR = 13,
    WEAPON_M249 = 14,
    WEAPON_M4A4 = 16,
    WEAPON_MAC10 = 17,
    WEAPON_P90 = 19,
    WEAPON_UMP45 = 24,
    WEAPON_XM1014 = 25,
    WEAPON_BIZON = 26,
    WEAPON_MAG7 = 27,
    WEAPON_NEGEV = 28,
    WEAPON_SAWEDOFF = 29,
    WEAPON_TEC9 = 30,
    WEAPON_TASER = 31,
    WEAPON_HKP2000 = 32,
    WEAPON_MP7 = 33,
    WEAPON_MP9 = 34,
    WEAPON_NOVA = 35,
    WEAPON_P250 = 36,
    WEAPON_SCAR20 = 38,
    WEAPON_SG556 = 39,
    WEAPON_SSG08 = 40,
    WEAPON_KNIFE = 42,
    WEAPON_FLASHBANG = 43,
    WEAPON_HEGRENADE = 44,
    WEAPON_SMOKEGRENADE = 45,
    WEAPON_MOLOTOV = 46,
    WEAPON_DECOY = 47,
    WEAPON_INCGRENADE = 48,
    WEAPON_C4 = 49,
    WEAPON_KNIFE_T = 59,
    WEAPON_M4A1_SILENCER = 60,
    WEAPON_USP_SILENCER = 61,
    WEAPON_CZ75A = 63,
    WEAPON_REVOLVER = 64,
    WEAPON_KNIFE_BAYONET = 500,
    WEAPON_KNIFE_FLIP = 505,
    WEAPON_KNIFE_GUT = 506,
    WEAPON_KNIFE_KARAMBIT = 507,
    WEAPON_KNIFE_M9_BAYONET = 508,
    WEAPON_KNIFE_TACTICAL = 509,
    WEAPON_KNIFE_FALCHION = 512,
    WEAPON_KNIFE_BOWIE = 514,
    WEAPON_KNIFE_BUTTERFLY = 515,
    WEAPON_KNIFE_PUSH = 516
};
IniParser.h:
C++:
/// IniParser.h
// Defines quick class to parse ini files with win api
#pragma once
#include "Common.h"
#pragma warning(disable:4800)

// Creds: TeamGamerFood, maybe? Can't remember exactly

class CIniParser
{
public:

    std::string szFile;

    std::string ReadString(const char* szSection, const char* szKey)
    {
        char szTemp[128];
        if (GetPrivateProfileStringA(szSection, szKey, "", szTemp, 128, szFile.c_str()))
        {
            char szString[128];
            sprintf(szString, szTemp);
            return std::string(szString);
        }
        return nullptr;
    }

    int ReadInt(const char* szSection, const char* szKey)
    {
        char szTemp[128];
        if (GetPrivateProfileStringA(szSection, szKey, "", szTemp, 128, szFile.c_str()))
        {
            char szString[128];
            sprintf(szString, szTemp);

            return std::stoi(szString);
        }
        return -1;
    }

    bool ReadBool(const char* szSection, const char* szKey)
    {
        char szTemp[128];
        if (GetPrivateProfileStringA(szSection, szKey, "", szTemp, 128, szFile.c_str()))
        {
            char szString[128];
            sprintf(szString, szTemp);
            return std::stoi(szString);
        }
        return false;
    }

    float ReadFloat(const char* szSection, const char* szKey)
    {
        char szTemp[128];
        if (GetPrivateProfileStringA(szSection, szKey, "", szTemp, 128, szFile.c_str()))
        {
            char szString[128];
            sprintf(szString, szTemp);

            return std::stof(szString);
        }
        return -1.f;
    }

    void Write(const char* szSection, const char* szKey, const char* szValue)
    {
        WritePrivateProfileStringA(szSection, szKey, szValue, szFile.c_str());
    }

    void WriteInt(const char* szSection, const char* szKey, int szValue)
    {
        WritePrivateProfileStringA(szSection, szKey, std::to_string(szValue).c_str(), szFile.c_str());
    }

    void WriteFloat(const char* szSection, const char* szKey, float szValue)
    {
        WritePrivateProfileStringA(szSection, szKey, std::to_string(szValue).c_str(), szFile.c_str());
    }

    bool SectionExists(const char* szSection)
    {
        char result[100];
        long lRetValue = GetPrivateProfileString(szSection, NULL, "", result, 90, szFile.c_str());
        return (lRetValue > 0);
    }

};

extern CIniParser* IniParser;
Main.cpp:
C++:
#include <iostream>
#include "Common.h"
#include "IniParser.h"

using namespace std;
int main()
{
    int key;
    CIniParser* IniParser = new CIniParser();
    IniParser->szFile = ExePath() + "\\Config.ini";

    if (!FileExists(IniParser->szFile))
    {
        IniParser->WriteInt(std::to_string(7).c_str(), "bind", 32);
           IniParser->Write(std::to_string(7).c_str(), "login", "123abc");
        IniParser->Write(std::to_string(7).c_str(), "password", "abc123abc");
    }
    key = IniParser->ReadInt(std::to_string(7).c_str(), "bind");
    char* keyss = _strdup(IniParser->ReadString(std::to_string(7).c_str(), "login").c_str());
    char* keye = _strdup(IniParser->ReadString(std::to_string(7).c_str(), "password").c_str());

    std::cout << key << std::endl;
    std::cout << keyss << std::endl;
    std::cout << keye << std::endl;
    system("pause");
    return 0;
}
Скачать исходник:
Пожалуйста, авторизуйтесь для просмотра ссылки.
 
Последнее редактирование:
Сверху Снизу