//Function that safely converts a 'WCHAR' String to 'LPSTR':
char* ConvertLPWSTRToLPSTR(LPWSTR lpwszStrIn)
{
LPSTR pszOut = NULL;
if (lpwszStrIn != NULL)
{
int nInputStrLen = wcslen(lpwszStrIn);
// Double NULL Termination
int nOutputStrLen = WideCharToMultiByte(CP_ACP, 0, lpwszStrIn, nInputStrLen, NULL, 0, 0, 0) + 2;
pszOut = new char[nOutputStrLen];
if (pszOut)
{
memset(pszOut, 0x00, nOutputStrLen);
WideCharToMultiByte(CP_ACP, 0, lpwszStrIn, nInputStrLen, pszOut, nOutputStrLen, 0, 0);
}
}
return pszOut;
}
clearTime = iniReader.ReadString("Gloable", "lastClearTime", "2022-06-17 10:22:34");
time_t t = StringToDatetime(clearTime);
lastClearTime = CTime(t); //上次删除时间
//方法1
/*IniWriter iniWriter(".\\config.ini");
time_t curT;
time(&curT);
struct tm* timeinfo;
timeinfo = localtime(&curT);
char buffer[80];
strftime(buffer, 80, "%F %T", timeinfo);
iniWriter.WriteString("Gloable", "lastClearTime", buffer);*/
//方法2:
//CTime tNowTime = CTime::GetCurrentTime();
//tNowTime = tNowTime - CTimeSpan(fileNdays, 0, 0, 0); // 指向nDays天前的日期 1655169307
//IniWriter iniWriter(".\\config.ini");
//CString result = tNowTime.Format("%Y-%m-%d %H:%M:%S");
// //USES_CONVERSION;
// //LPCSTR wszTime = W2A(result);//while循环中不要使用
//LPCSTR wszTime = ConvertLPWSTRToLPSTR(result.GetBuffer(result.GetLength()));//可在while循环中使用
//result.ReleaseBuffer();
//iniWriter.WriteString("Gloable", "lastClearTime", (LPSTR)wszTime);//(LPSTR)(LPCTSTR)result;
IniWriter.h
#ifndef INIFILE_INIWRITER_H
#define INIFILE_INIWRITER_H
class IniWriter
{
public:
IniWriter(char* szFileName);
void WriteInteger(char* szSection, char* szKey, int iValue);
void WriteFloat(char* szSection, char* szKey, float fltValue);
void WriteBoolean(char* szSection, char* szKey, bool bolValue);
void WriteString(char* szSection, char* szKey, char* szValue);
private:
char m_szFileName[255];
};
#endif
IniWriter.cpp
#include "IniWriter.h"
#include <iostream>
#include <Windows.h>
IniWriter::IniWriter(char* szFileName)
{
memset(m_szFileName, 0x00, 255);
memcpy(m_szFileName, szFileName, strlen(szFileName));
}
void IniWriter::WriteInteger(char* szSection, char* szKey, int iValue)
{
char szValue[255];
sprintf(szValue, "%d", iValue);
WritePrivateProfileStringA(szSection, szKey, szValue, m_szFileName);
}
void IniWriter::WriteFloat(char* szSection, char* szKey, float fltValue)
{
char szValue[255];
sprintf(szValue, "%f", fltValue);
WritePrivateProfileStringA(szSection, szKey, szValue, m_szFileName);
}
void IniWriter::WriteBoolean(char* szSection, char* szKey, bool bolValue)
{
char szValue[255];
sprintf(szValue, "%s", bolValue ? "True" : "False");
WritePrivateProfileStringA(szSection, szKey, szValue, m_szFileName);
}
void IniWriter::WriteString(char* szSection, char* szKey, char* szValue)
{
WritePrivateProfileStringA(szSection, szKey, szValue, m_szFileName);
}
参考: