GetPrivateProfileString 要如何获取所有配置文件啊?

a6640002 2017-07-11 03:11:43
C++

DWORD WINAPI GetPrivateProfileString(
_In_ LPCTSTR lpAppName,
_In_ LPCTSTR lpKeyName,
_In_ LPCTSTR lpDefault,
_Out_ LPTSTR lpReturnedString,
_In_ DWORD nSize,
_In_ LPCTSTR lpFileName
);



lpAppName [in]
The name of the section containing the key name. If this parameter is NULL, the GetPrivateProfileString function copies all section names in the file to the supplied buffer.
lpKeyName [in]
The name of the key whose associated string is to be retrieved. If this parameter is NULL, all key names in the section specified by the lpAppName parameter are copied to the buffer specified by the lpReturnedString parameter.
lpDefault [in]
A default string. If the lpKeyName key cannot be found in the initialization file, GetPrivateProfileString copies the default string to the lpReturnedString buffer. If this parameter is NULL, the default is an empty string, "".
Avoid specifying a default string with trailing blank characters. The function inserts a null character in the lpReturnedString buffer to strip any trailing blanks.
lpReturnedString [out]
A pointer to the buffer that receives the retrieved string.
nSize [in]
The size of the buffer pointed to by the lpReturnedString parameter, in characters.
lpFileName [in]
The name of the initialization file. If this parameter does not contain a full path to the file, the system searches for the file in the Windows directory.



我看msdn上是说填写个NULL就可以加载所有的选项到缓冲区了,
为什么我在vc6上写个null,读取出来的还是default那项啊?是因为sdk版本问题么?vc6的sdk不支持遍历所有配置选项么?
...全文
348 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
zgl7903 2017-07-13
  • 打赏
  • 举报
回复
在 CXXXXApp::InitInstance 中设置好 .ini 文件名,

CXXXXApp::InitInstance()
{
  ……

  // Change the registry key under which our settings are stored.
  // TODO: You should modify this string to be something appropriate
  // such as the name of your company or organization.
  {
    //SetRegistryKey(_T("Local AppWizard-Generated Applications"));

    //First free the string allocated by MFC at CWinApp startup.
    //The string is allocated before InitInstance is called.
    free((void*)m_pszProfileName);
    //Change the name of the .INI file.
    //The CWinApp destructor will free the memory.
    m_pszProfileName=_tcsdup(_T("d:\\somedir\\myini.ini"));
  }

  ……
剩下的 就正常用了 AfxGetApp()->GetProfileXXXX AfxGetApp()->WriteProfileXXXX
战在春秋 2017-07-11
  • 打赏
  • 举报
回复
如果只有一个section,list列表可以用来接收对应此appname下的各项。 可以看下我之前发的例子: https://vcpptips.wordpress.com/tag/getprivateprofilestring/
a6640002 2017-07-11
  • 打赏
  • 举报
回复
是这样的,我想把这个ini临时做数据库用,想在程序执行的时候默认把这些数据送到list列表里,请问这个能直接用这个api实现把文件里的文件按appname开头,一条一条输出到list列表里么?
开心秋水 2017-07-11
  • 打赏
  • 举报
回复
D:\\abc.txt里面的数据可能是这样的: [MySection] close=123 open=100 [Second] hello=yes sss=yyy xxx=zzzzz02h
开心秋水 2017-07-11
  • 打赏
  • 举报
回复

char s[20] = { 0 };
GetPrivateProfileStringA("MySection", "Close", "0", s, sizeof(s), "D:\\abc.txt");//读取ini的MySection节的Close字段的值到s字符串中,如果没有读取到值,则设置默认值为"0"
战在春秋 2017-07-11
  • 打赏
  • 举报
回复
ini文件是如下结构:
[sectoin1] //AppName 
xxx = ... //KeyName 
yyy = ....//KeyName
GetPrivateProfileString函数中AppName,KeyName两个参数都可以设置为NULL,不清楚你指的是哪一个参数。 这两个参数为NULL时如何处理,可以再仔细看下MSDN。 可以参考下面这篇文章(实现了一个读写INI文件的类) https://vcpptips.wordpress.com/tag/getprivateprofilestring/

16,466

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧