读写配置文件

来源:互联网 发布:收银软件培训视频 编辑:程序博客网 时间:2024/05/21 10:53

WritePrivateProfileString

求助编辑百科名片

WritePrivateProfileS  
WritePrivateProfileS
WritePrivateProfileString函数名称,多用于VB、VC中使用,函数声明:Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long

目录

函数在VB中的使用
函数在VC中的使用
补充信息

编辑本段函数在VB中的使用

所有版本通用:WritePrivateProfileString(lpApplicationName, lpKeyName, lpString, lpFileName) 
VB声明
Declare Function WritePrivateProfileString& Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As String, ByVal lpFileName As String)
说明
在初始化文件指定小节内设置一个字串
返回值
Long,非零表示成功,零表示失败。会设置GetLastError
参数表
参数
类型及说明
lpApplicationName
String,要在其中写入新字串的小节名称。这个字串不区分大小写
lpKeyName
Any,要设置的项名或条目名。这个字串不区分大小写。用vbNullString可删除这个小节的所有设置项
lpString
String,指定为这个项写入的字串值。用vbNullString表示删除这个项现有的字串
lpFileName
String,初始化文件的名字。如果没有指定完整路径名,则windows会在windows目录查找文件。如果文件没有找到,则函数会创建它
相关函数:GetPrivateProfileString

编辑本段函数在VC中的使用

在我们写的程序当中,总有一些配置信息需要保存下来,以便完成程序的功能,最简单的办法就是将这些信息写入INI文件中,程序初始化时再读入.具体应用如下:
一.将信息写入.INI文件中
1.所用的WINAPI函数原型为:
BOOL WritePrivateProfileString(
LPCTSTR lpAppName,
LPCTSTR lpKeyName,
LPCTSTR lpString,
LPCTSTR lpFileName
);
其中各参数的意义
LPCTSTR lpAppName 是INI文件中的一个字段名.
LPCTSTR lpKeyName 是lpAppName下的一个键名,通俗讲就是变量名.
LPCTSTR lpString 是键值,也就是变量的值,不过必须为LPCTSTR型或CString型的.
LPCTSTR lpFileName 是完整的INI文件名.
2.具体使用方法:设现有一名学生,需把他的姓名和年龄写入 c:\stud\student.ini 文件中.
CString strName,strTemp;
int nAge;
strName="张三";
nAge=12;
::WritePrivateProfileString("StudentInfo","Name",strName,"c:\\stud\\student.ini");
此时c:\stud\student.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中的意义一样.
lpDefault : 如果INI文件中没有前两个参数指定的字段名或键名,则将此值赋给变量.
lpReturnedString : 接收INI文件中的值的CString对象,即目的缓存器.
nSize : 目的缓存器的大小.
lpFileName : 是完整的INI文件名.
2.具体使用方法:现要将上一步中写入的学生的信息读入程序中.
CString strStudName;
int nStudAge;
GetPrivateProfileString("StudentInfo","Name","默认姓名",strStudName.GetBuffer(MAX_PATH),MAX_PATH,"c:\\stud\\student.ini");
执行后 strStudName 的值为:"张三",若前两个参数有误,其值为:"默认姓名".
注意:如果在读入的ini文件不存在,则按默认值生成相应的ini文件
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;
文件://共有6个文件名需要保存
for(i=0;i {strTemp.format("%d",i);
strTempA=文件名;
文件://文件名可以从数组,列表框等处取得.
::WritePrivateProfileString("UseFileName","FileName"+strTemp,strTempA,
"c:\\usefile\\usefile.ini");
}
strTemp.format("%d",nCount);
::WritePrivateProfileString("FileCount","Count",strTemp,"c:\\usefile\\usefile.ini");
文件://将文件总数写入,以便读出.
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");
文件://使用strTempA中的内容.
}



配置文件中经常用到ini文件,在VC中其函数分别为:
写入.ini文件:

[cpp] view plaincopy
  1. BOOL WritePrivateProfileString(  
  2.   LPCTSTR lpAppName,  // INI文件中的一个字段名[节名]可以有很多个节名  
  3.   
  4.   LPCTSTR lpKeyName,  // lpAppName 下的一个键名,也就是里面具体的变量名  
  5.   
  6.   LPCTSTR lpString,   // 键值,也就是数据  
  7.   
  8.   LPCTSTR lpFileName  // INI文件的路径  
  9. );  


 

读取.ini文件:

[cpp] view plaincopy
  1. DWORD GetPrivateProfileString(  
  2.   LPCTSTR lpAppName,        // INI文件中的一个字段名[节名]可以有很多个节名  
  3.   
  4.   LPCTSTR lpKeyName,        // lpAppName 下的一个键名,也就是里面具体的变量名  
  5.   
  6.   LPCTSTR lpDefault,        // 如果lpReturnedString为空,则把个变量赋给lpReturnedString  
  7.   
  8.   LPTSTR lpReturnedString,  // 存放键值的指针变量,用于接收INI文件中键值(数据)的接收缓冲区  
  9.   
  10.   DWORD nSize,            // lpReturnedString的缓冲区大小  
  11.   
  12.   LPCTSTR lpFileName        // INI文件的路径  
  13. );  


 

读取整形值:(返回值为读到的整)

[cpp] view plaincopy
  1. UINT GetPrivateProfileInt(  
  2.   LPCTSTR lpAppName,  // INI文件中的一个字段名[节名]可以有很多个节名  
  3.   LPCTSTR lpKeyName,  // lpAppName 下的一个键名,也就是里面具体的变量名  
  4.   INT nDefault,       // 如果没有找到指定的数据返回,则把个变量值赋给返回值  
  5.   
  6.   LPCTSTR lpFileName  // INI文件的路径  
  7.   
  8. );  



读写INI文件时相对路径和绝对路径都可以,根据实际情况选择

"..\\IniFileName.ini"    // 这样的为相对路径

"D:\\IniFileName.ini"    // 这样的为绝对路径

 

MAX_PATH:是微软最大路径占的字节所设的宏


例子:

写INI文件:

 

[cpp] view plaincopy
  1. LPTSTR lpPath = new char[MAX_PATH];  
  2.    
  3. strcpy(lpPath, "D:\\IniFileName.ini");  
  4.   
  5. WritePrivateProfileString("LiMing""Sex""Man", lpPath);  
  6. WritePrivateProfileString("LiMing""Age""20", lpPath);  
  7.    
  8. WritePrivateProfileString("Fangfang""Sex""Woman", lpPath);  
  9. WritePrivateProfileString("Fangfang""Age""21", lpPath);  
  10.   
  11.    
  12.   
  13. delete [] lpPath;  
  14.   
  15.   
  16. INI文件如下:  
  17.   
  18. [LiMing]  
  19. Sex=Man  
  20. Age=20  
  21. [Fangfang]  
  22. Sex=Woman  
  23. Age=21  
  24.   
  25. 读INI文件:  
  26.   
  27.    
  28.   
  29. LPTSTR lpPath = new char[MAX_PATH];  
  30. LPTSTR LiMingSex = new char[6];  
  31. int LiMingAge;  
  32. LPTSTR FangfangSex = new char[6];  
  33. int FangfangAge;  
  34.    
  35.   
  36. strcpy(lpPath, "..\\IniFileName.ini");  
  37.    
  38. GetPrivateProfileString("LiMing""Sex""", LiMingSex, 6, lpPath);  
  39. LiMingAge = GetPrivateProfileInt("LiMing""Age", 0, lpPath);  
  40.    
  41. GetPrivateProfileString("Fangfang""Sex""", FangfangSex, 6, lpPath);  
  42. FangfangAge = GetPrivateProfileInt("Fangfang""Age", 0, lpPath);  
  43.   
  44.    
  45.   
  46. delete [] lpPath;