1、写入ini文件
CString strCount = "123456789";//写入参数
TCHAR exeFullPath[MAX_PATH]; // Full path
GetModuleFileName(NULL, exeFullPath, MAX_PATH);
std::wstring strFullPath = (std::wstring)(exeFullPath);
int nStart = strFullPath.find_last_of(TEXT("\\"));
CString exe_Path = exeFullPath;
exe_Path = exe_Path.Mid(0, nStart + 1) +"config.ini";//exe所在路径
//exe_Path = exe_Path.Mid(nStart + 1, strFullPath.length() - 4);//exe完成名称
if (WritePrivateProfileString(L"Device", L"Path", strCount, exe_Path))
{
}
CString strCount = "123456789";//写入参数
TCHAR exeFullPath[MAX_PATH]; // Full path
GetModuleFileName(NULL, exeFullPath, MAX_PATH);
std::wstring strFullPath = (std::wstring)(exeFullPath);
int nStart = strFullPath.find_last_of(TEXT("\\"));
CString exe_Path = exeFullPath;
exe_Path = exe_Path.Mid(0, nStart + 1) +"config.ini";//exe所在路径
//exe_Path = exe_Path.Mid(nStart + 1, strFullPath.length() - 4);//exe完成名称
if (WritePrivateProfileString(L"Device", L"Path", strCount, exe_Path))
{
}
2、读取ini文件
char chStudentName[MAX_PATH];
CString strCount = "";//获取内容
TCHAR exeFullPath[MAX_PATH]; // Full path
GetModuleFileName(NULL, exeFullPath, MAX_PATH);
std::wstring strFullPath = (std::wstring)(exeFullPath);
int nStart = strFullPath.find_last_of(TEXT("\\"));
CString exe_Path = exeFullPath;
exe_Path = exe_Path.Mid(0, nStart + 1) + "config.ini";//exe所在路径
GetPrivateProfileString(L"Device", L"Path", strCount, strCount.GetBuffer(MAX_PATH), MAX_PATH, exe_Path);//第一个strCount为如果没有值就赋值默认值strCount,
strCount.ReleaseBuffer();//GetBuffer后需要ReleaseBuffer才能使用GetLength获取长度
char chStudentName[MAX_PATH];
CString strCount = "";//获取内容
TCHAR exeFullPath[MAX_PATH]; // Full path
GetModuleFileName(NULL, exeFullPath, MAX_PATH);
std::wstring strFullPath = (std::wstring)(exeFullPath);
int nStart = strFullPath.find_last_of(TEXT("\\"));
CString exe_Path = exeFullPath;
exe_Path = exe_Path.Mid(0, nStart + 1) + "config.ini";//exe所在路径
GetPrivateProfileString(L"Device", L"Path", strCount, strCount.GetBuffer(MAX_PATH), MAX_PATH, exe_Path);//第一个strCount为如果没有值就赋值默认值strCount,
strCount.ReleaseBuffer();//GetBuffer后需要ReleaseBuffer才能使用GetLength获取长度
本文介绍了如何在Windows环境中使用C++通过CString和std::wstring操作写入和读取ini文件,包括GetModuleFileName、FindLastOf函数及WritePrivateProfileString和GetPrivateProfileString的使用实例。
1479

被折叠的 条评论
为什么被折叠?



