// ini file read and write sample. support wchar_t. // write C:\Users\(USERNAME)\AppData\Roaming\dev_ini_file_rw\dev_ini_file_rw.ini // // Windows10 x64 22H2 + MinGW (gcc 6.3.0) // Last updated: <2024/01/19 00:19:02 +0900> // use SHGetSpecialFolderPath() #define _WIN32_IE 0x0400 #include #include #include #include #include #include #include // target directory #define INIDIR _T("dev_ini_file_rw") // target ini file name #define INIFILENAME _T("dev_ini_file_rw.ini") // section name #define SECNAME _T("ssstarsgl_config") // global work int wait = 15; int speed = 1000; int number = 500; int fps_display = 0; // write or create ini file void writeIni(TCHAR *filepath) { TCHAR buf[1024]; wsprintf(buf, _T("%d"), wait); WritePrivateProfileString(SECNAME, _T("wait"), buf, filepath); wsprintf(buf, _T("%d"), speed); WritePrivateProfileString(SECNAME, _T("speed"), buf, filepath); wsprintf(buf, _T("%d"), number); WritePrivateProfileString(SECNAME, _T("number"), buf, filepath); wsprintf(buf, _T("%d"), fps_display); WritePrivateProfileString(SECNAME, _T("fps_display"), buf, filepath); } // read ini file void readIni(TCHAR *filepath) { wait = GetPrivateProfileInt(SECNAME, _T("wait"), -1, filepath); speed = GetPrivateProfileInt(SECNAME, _T("speed"), -1, filepath); number = GetPrivateProfileInt(SECNAME, _T("number"), -1, filepath); fps_display = GetPrivateProfileInt(SECNAME, _T("fps_display"), -1, filepath); } int main(void) { TCHAR filepath[MAX_PATH]; // target ini file path TCHAR waFolderPath[MAX_PATH]; // APPDATA folder path TCHAR waTgtFolderPath[MAX_PATH]; // target folder path // get APPDATA folder path // request shlobj.h SHGetSpecialFolderPath(NULL, waFolderPath, CSIDL_APPDATA, FALSE); // wprintf(L"APPDATA folder path: [%S]\n", waFolderPath); if (PathCombine(waTgtFolderPath, waFolderPath, INIDIR) == NULL) { printf("ERROR: Can not make target folder path.\n"); return -1; } // wprintf(L"Target folder path: [%S]\n", waTgtFolderPath); { BOOL create_dir = FALSE; if (!PathFileExists(waTgtFolderPath)) { printf("Not found target folder. create.\n"); create_dir = TRUE; } else if (!PathIsDirectory(waTgtFolderPath)) { printf("Found target folder path. but, not directory. create.\n"); create_dir = TRUE; } if (create_dir) { if (!CreateDirectory(waTgtFolderPath, NULL)) { printf("ERROR: Can not create target folder.\n"); return -1; } } } if (PathCombine(filepath, waTgtFolderPath, INIFILENAME) == NULL) { printf("ERROR: Can not make ini file path.\n"); return -1; } // %s ... ANSI // %S ... UNICODE wprintf(L"ini file path : [%S]\n", filepath); if (!PathFileExists(filepath)) { // Not found ini file printf("Not found ini file. Create.\n"); writeIni(filepath); } if (!PathFileExists(filepath)) { printf("Not found ini file.\n"); return -1; } // read ini file printf("Found ini file. Read.\n"); readIni(filepath); // dump results printf("wait=%d\n", wait); printf("speed=%d\n", speed); printf("number=%d\n", number); printf("fps_display=%d\n", fps_display); return 0; }