读取文件
#include "read.h"
total_region total;
A1_region A1;
A2_region A2;
A3_region A3;
A1_Small_area small_area;
string outline_image_path;
CString ini_path = "D://a.ini";
//QString转为const wchar_t*
inline const WCHAR *QStoWCHAR(const QString& qs)
{
return (const WCHAR *)qs.utf16();
}
// TCHAR * 转为char *
char* TCHARToChar(TCHAR* pTchar)
{
char* pChar = nullptr;
int nLen = wcslen(pTchar) + 1;
pChar = new char[nLen * 2];
WideCharToMultiByte(CP_ACP, 0, pTchar, nLen, pChar, 2 * nLen, NULL, NULL);
return pChar;
}
//TCHAR 转换 string
string TCHAR2STRING(TCHAR * STR)
{
int iLen = WideCharToMultiByte(CP_ACP, 0,STR, -1, NULL, 0, NULL, NULL);
char* chRtn = new char[iLen * sizeof(char)];
WideCharToMultiByte(CP_ACP, 0, STR, -1, chRtn, iLen, NULL, NULL);
string str(chRtn);
return str;
}
//读取整型ini数据
int getInt(CString lpKeyName, INT nDefault)
{
int value = GetPrivateProfileInt(TEXT("Detection"), lpKeyName, nDefault, ini_path);
return value;
}
//读取浮点型ini数据
double getDouble(CString lpKeyName, CString nDefault)
{
//读取字符型ini数据
TCHAR value[20];
GetPrivateProfileString(TEXT("Detection"), lpKeyName, nDefault, value, sizeof(value), ini_path);
//转为double型
return strtod(TCHARToChar(value), NULL);
}
void read_ini()
{
total.model_flag = getInt("model_flag", 0);
total.filter_flag = getInt("model_flag", 0);
total.Zoom_factor_x = getDouble("Zoom_factor_x", "1");;
total.Zoom_factor_y = getDouble("Zoom_factor_y", "1");;
TCHAR file_path[200];
GetPrivateProfileString(TEXT("Detection"), CString("configuration_file_path "), CString(""), file_path, sizeof(file_path), ini_path);;
//修改char[]的值
strcpy_s(configuration_file_path, TCHARToChar(file_path));
GetPrivateProfileString(TEXT("Detection"), CString("outline_image_path"), CString(""), file_path, sizeof(file_path), ini_path);
//修改字符串
outline_image_path = TCHAR2STRING(file_path);
#ifdef DEBUG
cout << configuration_file_path << " * " << outline_image_path << endl;
#endif
}
参考链接:
1. TCHAR 转换 string
https://blog.csdn.net/wzy198852/article/details/19990567
2. TCHAR与CHAR互转
https://blog.csdn.net/pjf_1806339272/article/details/105967020
3. QString转换为LPCTSTR
https://xbuba.com/questions/51027141
注:二进制文件中不要保存string类型,读取的时候会出现都不完全
注意:.ini文件的格式必须是utf-8