zoukankan      html  css  js  c++  java
  • MFC VC++ 读写、遍历 ini 配置文件

    参考

    VC遍历INI文件

    其实是利用GetPrivateProfileString 函数传递不同参数读到的数据不同,从而实现功能

    DWORD 实际缓冲区数据长度 = GetPrivateProfileString(“字段名”, “变量名”, “读不到时缓冲区的值”, 提供缓冲区, 提供缓冲区长度, “ini文件全路径”);

    GetPrivateProfileString(NULL, NULL, NULL, 提供缓冲区, 提供缓冲区长度, “ini文件全路径”);

    此时读到缓冲区的数据是ini文件所有字段的名字

    GetPrivateProfileString(“字段名”, NULL, NULL, 提供缓冲区, 提供缓冲区长度, “ini文件全路径”);

    此时读到缓冲区的数据是该字段所有变量名

    GetPrivateProfileString(“字段名”, “变量名”, NULL, 提供缓冲区, 提供缓冲区长度, “ini文件全路径”);

    此时读到缓冲区的数据是该字段该变量名的变量值

    根据上面的特点可以有代码

     1 void xxxDlg::EnumIniFile(LPCTSTR pFilePath)
     2 {
     3     CString strTemp;
     4     TCHAR strReturn[100];
     5     //字段名 变量名 默认值 字符串缓冲区 长度 文件名和完整路径名
     6     DWORD dwAppNameSize = GetPrivateProfileString(NULL, NULL, NULL, strReturn, 100, pFilePath);
     7     if (dwAppNameSize > 0)
     8     {
     9         //此时strReturn的值是字段1 + '' 字段2 + '' 字段3 + ''  ...
    10         CString strTempAppName('@', dwAppNameSize);//分配100字节空间,用@填充
    11         for (DWORD i = 0; i < dwAppNameSize; i++)
    12         {
    13             if (strReturn[i] == '')
    14             {
    15                 strTempAppName.SetAt(i, '@');//CString 动态空间的话不能用SetAt增加空间
    16             }
    17             else
    18             {
    19                 strTempAppName.SetAt(i, strReturn[i]);
    20             }
    21         }
    22         int i_appPos = 1;
    23         CString strAppName;
    24         while (i_appPos)
    25         {
    26             i_appPos = strTempAppName.Find('@');
    27             if (i_appPos > 0)
    28             {
    29                 strAppName = strTempAppName.Left(i_appPos);
    30                 strTempAppName = strTempAppName.Right(strTempAppName.GetLength() - (i_appPos + 1));//把找过的截断                
    31                 //MessageBox(strAppName);//这里找到字段名
    32                 DWORD dwKeyNameSize = GetPrivateProfileString(strAppName, NULL, NULL, strReturn, 100, pFilePath);
    33                 if (dwKeyNameSize > 0)
    34                 {
    35                     //此时strReturn的值是变量名 + '' 变量名 + '' 变量名 + ''  ...
    36                     CString strKeyAppName('#', dwKeyNameSize);//分配xx字节空间,用#填充
    37                     for (DWORD i = 0; i < dwKeyNameSize; i++)
    38                     {
    39                         if (strReturn[i] == '')
    40                         {
    41                             strKeyAppName.SetAt(i, '#');//CString 动态空间的话不能用SetAt增加空间
    42                         }
    43                         else
    44                         {
    45                             strKeyAppName.SetAt(i, strReturn[i]);
    46                         }
    47                     }
    48                     int i_keyPos = 1;
    49                     CString strkeyName;
    50                     while (i_keyPos)
    51                     {
    52                         i_keyPos = strKeyAppName.Find('#');
    53                         if (i_keyPos > 0)
    54                         {
    55                             strkeyName = strKeyAppName.Left(i_keyPos);
    56                             strKeyAppName = strKeyAppName.Right(strKeyAppName.GetLength() - (i_keyPos + 1));//把找过的截断                
    57                             //MessageBox(strkeyName);//这里找到变量名
    58                             DWORD dwValueNameSize = GetPrivateProfileString(strAppName, strkeyName, NULL, strReturn, 100, pFilePath);
    59                             CString strValue;
    60                             if (dwValueNameSize > 0)
    61                             {
    62                                 //此时strReturn的值变量的值
    63                                 strValue = strReturn;
    64                             }
    65                             else
    66                             {
    67                                 strValue.Empty();
    68                             }
    69                             CString strMsg;
    70                             strMsg.Format(_T("%s = %s"), strkeyName, strValue);
    71                             MessageBox(strMsg);
    72                         }
    73                         else
    74                         {
    75                             break;
    76                         }
    77                     }
    78 
    79                 }
    80             }
    81             else
    82             {
    83                 break;
    84             }
    85         }
    86     }
    87 }

    如果读ini文件遇到中文乱码问题,先排除一下编码格式的问题

    一般用新建txt,改后缀名的方式建一个ini文件,这时编码格式不是ANSI 或 Unicode(我也不知道默认格式是啥)

    用另存为的方式修改ini文件的编码格式,改为ANSI或Unicode,我试过改成ANSI就可以了,Unicode我电脑没这个选项,不知道为啥

    这里和程序使用字符集 Unicode或多字节应该是没关系的,实际情况各自测试一下

  • 相关阅读:
    损失函数 代价函数 评分函数 目标函数
    python目录索引
    机器学习/深度学习资料合集
    Git笔记
    目标检测中的正负样本分配
    map计算
    nms
    08shell脚本
    07makefile文件
    05-STL
  • 原文地址:https://www.cnblogs.com/ckrgd/p/14135382.html
Copyright © 2011-2022 走看看