ini文件操作

首先,  一.将信息写入.INI文件中.

   1.所用的WINAPI函数原型为: 
                     BOOL WritePrivateProfileString(
                                                                       LPCTSTR lpAppName,
                                                                       LPCTSTR lpKeyName,
                                                                       LPCTSTR lpString,
                                                                       LPCTSTR lpFileName
                                                                ); 
  其中各参数的意义:
    LPCTSTR lpAppName 是INI文件中的一个字段名,例如:[student].
    LPCTSTR lpKeyName 是lpAppName下的一个键名,通俗讲就是变量名,例如:name.
    LPCTSTR lpString 是键值,也就是变量的值,不过必须为LPCTSTR型或CString型的,例如:“21”.
    LPCTSTR lpFileName 是完整的INI文件名,例如:c:\test.ini.

举例:设现有一名学生,需把他的姓名和年龄写入 c:studstudent.ini 文件中.

CString strName,strTemp;
int nAge;
strName="张三";
nAge=12;
::WritePrivateProfileString("StudentInfo","Name",strName,"c:\stud\student.ini");

此时c:studstudent.ini文件中的内容如下:
[StudentInfo]
Name=张三

3.要将学生的年龄保存下来,只需将整型的值变为字符型即可:
strTemp.Format("%d",nAge);
::WritePrivateProfileString("StudentInfo","Age",strTemp,"c:\stud\student.ini");

  二.将信息从INI文件中读入程序中的变量.
  1.所用的WINAPI函数原型为:
  DWORD GetPrivateProfileString(
                                                    LPCTSTR lpAppName, 
                                                    LPCTSTR lpKeyName, 
                                                    LPCTSTR lpDefault, 
                                                    LPTSTR lpReturnedString, 
                                                    DWORD nSize, 
                                                    LPCTSTR lpFileName 
                                                     ); 
  其中各参数的意义: 
   前二个参数与 WritePrivateProfileString中的意义一样.
   LPCTSTR  lpDefault : 如果INI文件中没有前两个参数指定的字段名或键名,则将此值赋给变量. 
   LPCTSTR  lpReturnedString : 接收INI文件中的值的CString对象,即目的缓存器.
   DWORD  nSize : 目的缓存器的大小.
   LPCTSTR  lpFileName : 是完整的INI文件名.
  2.具体使用方法:现要将上一步中写入的学生的信息读入程序中.
CString strStudName;
int nStudAge; 
GetPrivateProfileString("StudentInfo","Name","默认姓名",strStudName.GetBuffer(MAX_PATH),MAX_PATH,"c:\stud\student.ini"); 
  执行后 strStudName 的值为:"张三",若前两个参数有误,其值为:"默认姓名".
  3.读入整型值要用另一个WINAPI函数: 
UINT GetPrivateProfileInt(
                                         LPCTSTR lpAppName, 
                                         LPCTSTR lpKeyName, 
                                         INT nDefault, 
                                         LPCTSTR lpFileName 
                                          ); 
  这里的参数意义与上相同.使用方法如下:
nStudAge=GetPrivateProfileInt("StudentInfo","Age",10,"c:\stud\student.ini"); 
三.循环写入多个值,设现有一程序,要将最近使用的几个文件名保存下来,具体程序如下:

  1.写入:

CString strTemp,strTempA;
int i;
int nCount=6;
file://共有6个文件名需要保存
for(i=0;i {strTemp.Format("%d",i);
strTempA=文件名;
file://文件名可以从数组,列表框等处取得.
::WritePrivateProfileString("UseFileName","FileName"+strTemp,strTempA,
"c:\usefile\usefile.ini");
}
strTemp.Format("%d",nCount);
::WritePrivateProfileString("FileCount","Count",strTemp,"c:\usefile\usefile.ini");
file://将文件总数写入,以便读出.

  2.读出:

nCount=::GetPrivateProfileInt("FileCount","Count",0,"c:\usefile\usefile.ini");
for(i=0;i {strTemp.Format("%d",i);
strTemp="FileName"+strTemp;
::GetPrivateProfileString("CurrentIni",strTemp,"default.fil", strTempA.GetBuffer(MAX_PATH),MAX_PATH,"c:\usefile\usefile.ini");

file://使用strTempA中的内容.

}


   补充四点:

   1.INI文件的路径必须完整,文件名前面的各级目录必须存在,否则写入不成功,该函数返回 FALSE 值.

   2.文件名的路径中必须为 \ ,因为在VC++中, \ 才表示一个 .

   3.也可将INI文件放在程序所在目录,此时 lpFileName 参数为: ".\student.ini".

   4.从网页中粘贴源代码时,最好先粘贴至记事本中,再往VC中粘贴,否则易造成编译错误,开始时我也十分不解,好好的代码怎么就不对呢?后来才找到这个方法.还有一些代码中使用了全角字符如:<,\等,也会造成编译错误.

用API写INI文件的函数有
BOOL WritePrivateProfileString(
    LPCTSTR lpAppName, // 节点
    LPCTSTR lpKeyName, // 键名 
    LPCTSTR lpString, // 添加的字符串 
    LPCTSTR lpFileName  // Ini文件名 
   );

BOOL WritePrivateProfileStruct(
    LPCTSTR lpszSection, // 节点
    LPCTSTR lpszKey, // 键名

    LPVOID lpStruct, // 要写入的数据缓冲区
    UINT uSizeStruct, // 缓冲区的大小
    LPCTSTR szFile // ini文件名
   );
BOOL WritePrivateProfileSection(
    LPCTSTR lpAppName, // pointer to string with section name 
    LPCTSTR lpString, // 写入的字符串例如:“A=123”
    LPCTSTR lpFileName  // ini文件名
   );
用API读INI文件的函数有
DWORD GetPrivateProfileString(
    LPCTSTR lpAppName, // points to section name 
    LPCTSTR lpKeyName, // points to key name 
    LPCTSTR lpDefault, // 默认字符串 ,如果没有则返回该值
    LPTSTR lpReturnedString, // 返回的字符串 
    DWORD nSize, // 返回字符串的大小 
    LPCTSTR lpFileName  // ini文件名
   );
DWORD GetPrivateProfileSection(
    LPCTSTR lpAppName, // address of section name 
    LPTSTR lpReturnedString, // address of return buffer 
    DWORD nSize, // size of return buffer 
    LPCTSTR lpFileName  // address of initialization filename  
   );
UINT GetPrivateProfileInt(
    LPCTSTR lpAppName, // address of section name
    LPCTSTR lpKeyName, // address of key name
    INT nDefault, // return value if key name is not found
    LPCTSTR lpFileName  // address of initialization filename
   ); 
BOOL GetPrivateProfileStruct(
    LPCTSTR lpszSection, // address of section name
    LPCTSTR lpszKey, // address of key name
    LPVOID lpStruct, // address of return buffer
    UINT uSizeStruct, // size of return buffer
    LPCTSTR szFile // address of initialization filename
   );
DWORD GetPrivateProfileSectionNames(

    LPTSTR lpszReturnBuffer, // address of return buffer 
    DWORD nSize, // size of return buffer 
    LPCTSTR lpFileName // address of initialization filename
   );
当然还有如WriteProfileString,WriteProfileSection,WriteProfileSturct, GetProfileString,GetProfileStruct,GetProfileSectionNames,GetProfileInt,GetProfileSection但这些只对Win.ini有效
下面我们来学习它们的用法
WritePrivateProfileString函数是向ini文件中写入字符串,如
WritePrivateProfileString(Pchar('类型'),Pchar('API'),Pchar('API 真好!'),Pchar('c:example.ini'));
如果第二个参数是nil,那么该操作将删除该节
如果第三个参数为nil,那么该操作将删除该节中的所有键
如果在指定的文件中没有路径,那么它将在系统的目录寻找文件,如果不存在则建立

WritePrivateProfileSection是向文件中写入一整个键,其它键的格式为key = value,如
WritePrivateProfileSection(Pchar('类型'),Pchar('其它=123'),Pchar('c:example.ini'));
注意,该操作将删除该节中的所有键后在进行本次的写入

WritePrivateProfileStruct是向文件中写入一个结构,如
type
  TPerson = record
    Name:string;
    Age:integer;
  end;
var
   Per:TPerson;
WritePrivateProfileStruct(Pchar('类型'),Pchar('结构'),@Per,Sizeof(Per),Pchar('C:example.ini'));

GetPrivateProfileString是从文件中读出一个指定键的字符串,如果没有找到,则返回默认值
GetPrivateProfileString(Pchar(‘类型'),Pchar('API'),Pchar('no value'),Str1,21,Pchar('c:example.ini'));

GetPrivateProfileSection是从文件中读出一整个键
GetprivateProfileSection('类型',str1,200,'c:example.ini');

GetPrivateProfileInt是从文件中读出指定键的整型值,如果没找到或不是整型的值,则返回默认值,如果返回值小于0,则返回0,如
i:=GetPrivateProfileInt(Pchar('类型'),Pchar('整型'),i,Pchar('C:example.ini'));
showMessage(inttostr(i));

GetPrivateProfileStruct从文件中读出指定键的结构值,如
type
  TPerson = record
    Name:string;
    Age:integer;
  end;
var
   Buffer:TPerson;
GetPrivateProfileStruct(Pchar('类型'),Pchar('结构'),@Buffer,Sizeof(Per),Pchar('C:example.ini'));

GetPrivateProfileSectionNames是从文件中读出所有节的名称,该函数返回读入缓冲区的字符数,如
count:=GetPrivateProfileSectionNames(Str3,200,Pchar('c:example.ini'));
ShowMessage(Str3);
此处显示的只是第一个节的名称,如果要显示第二个字符的名称则要用到下面这句
showmessage(str3+5);//字符串偏移到下一个字符串开始处


通过一下方法可以查找到绝对地址

CString des="";
 ::GetCurrentDirectory(_MAX_PATH,des.GetBuffer(_MAX_PATH));
 des.ReleaseBuffer();
 des+="\com.ini";

原文地址:https://www.cnblogs.com/liaocheng/p/4261623.html