请教 GetPrivateProfileSectionNames()用法(在线,解决给分!)

sun_wyz 2003-08-11 09:41:24
读取ini文件,例如:
[section1]
value1=....;
value2=....;
[section2]
value1=....;
value2=....;
我不知道[sectioni]的名称和数量。查msdn发现GetPrivateProfileSectionNames()
可以达到目的。
我的程序如下:
char section[256];
GetPrivateProfileSectionNames(
section, // return buffer
256, // size of return buffer
datafile.c_str() // initialization file name
);
但只能返回第一个section的值,
请大家帮忙!!!
...全文
1466 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
sun_wyz 2003-08-11
  • 打赏
  • 举报
回复
ok ok ok,
大侠厉害。

刚才真的好糊涂,呵呵:)
多谢二位星星,30分很少,不好意思了^_^
kingfish 2003-08-11
  • 打赏
  • 举报
回复
int nRet = GetPrivateProfileSectionNames(...)
for(i=0;i<nRet;i++) //sizeof(buf)>nRet 后面当然是乱码了
sun_wyz 2003-08-11
  • 打赏
  • 举报
回复
我是按照字符一个一个判断的,为什么出现乱码??
int i;
for(i=0;i<sizeof(buf);i++)
{
if(buf[i]!='\0')
Memo1->Lines->Add(buf[i]);
else
Memo1->Lines->Add("\\\\");
}
我只是作个试验,但结果的后面却出现了乱码。
而我的section里没有中文。
为什么??
jishiping 2003-08-11
  • 打赏
  • 举报
回复
GetPrivateProfileSectionNames() 得到的名字放在第一个参数指定的内存中,每个名字之
间用一个0字符隔开,最后一个名字用两个0字符作为结尾。这是Windows API函数处理多个名
字的通用方法。比如:
char buf[512];
GetPrivateProfileSectionNames(buf, sizeof(buf), IniFile);
for(char* Name=buf; *Name!='\0'; Name+=strlen(Name)+1)
ShowMessage(Name);
sun_wyz 2003-08-11
  • 打赏
  • 举报
回复
我试试分解数据
kingfish 2003-08-11
  • 打赏
  • 举报
回复
GetPrivateProfileSectionNames读出来的是用\0分隔的

如:section1\0section2
kingfish 2003-08-11
  • 打赏
  • 举报
回复
用TIniFile::ReadSections(TStrings *)
很方便
ini读写代码详解实例,有很详细注释,保证满意; 预览截取一部代码: ///    /// 读取INI文件中指定INI文件中的所有节点名称(Section)   ///    /// Ini文件   /// 所有节点,没有内容返回string[0]   public static string[] INIGetAllSectionNames(string iniFile) { uint MAX_BUFFER = 32767; //默认为32767   string[] sections = new string[0]; //返回值   //申请内存   IntPtr pReturnedString = Marshal.AllocCoTaskMem((int)MAX_BUFFER * sizeof(char)); uint bytesReturned = GetPrivateProfileSectionNames(pReturnedString, MAX_BUFFER, iniFile); if (bytesReturned != 0) { //读取指定内存的内容   string local = Marshal.PtrToStringAuto(pReturnedString, (int)bytesReturned).ToString(); //每个节点之间用\0隔,末尾有一个\0   sections = local.Split(new char[] { '\0' }, StringSplitOptions.RemoveEmptyEntries); } //释放内存   Marshal.FreeCoTaskMem(pReturnedString); return sections; } ///    /// 获取INI文件中指定节点(Section)中的所有条目(key=value形式)   ///    /// Ini文件   /// ection">节点名称   /// 指定节点中的所有项目,没有内容返回string[0]   public static string[] INIGetAllItems(string iniFile, string section) { //返回值形式为 key=value,例如 Color=Red   uint MAX_BUFFER = 32767; //默认为32767   string[] items = new string[0]; //返回值   //配内存   IntPtr pReturnedString = Marshal.AllocCoTaskMem((int)MAX_BUFFER * sizeof(char)); uint bytesReturned = GetPrivateProfileSection(section, pReturnedString, MAX_BUFFER, iniFile); if (!(bytesReturned == MAX_BUFFER - 2) || (bytesReturned == 0)) { string returnedString = Marshal.PtrToStringAuto(pReturnedString, (int)bytesReturned); items = returnedString.Split(new char[] { '\0' }, StringSplitOptions.RemoveEmptyEntries); } Marshal.FreeCoTaskMem(pReturnedString); //释放内存   return items; }

13,870

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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