典型的 .ini文件基本结构:节名,键名,以及键值三部分组成。
例如:UserInfo.ini 文件中,
[UserInfo] //节名
UserName=stevin //键名=键值
Password=123 //键名=键值
上面就是INI文件的结构了,当然了,一个文件中可以有多组节名,这里只举例说一个节的情况下,微软提供了如下几个函数,官方定义如下:
1.WritePrivateProfileString function:用于向INI文件中写入一个字符串数据。
Copies a string into the specified section of an initialization file.
Note This function is provided only for compatibility with 16-bit versions of Windows. Applications should store initialization
information in the registry.
Syntax
C++
BOOL WINAPI WritePrivateProfileString(
_In_ LPCTSTR lpAppName, //节名。如果INI文件中节名不存在,将创建一个节名
_In_ LPCTSTR lpKeyName, //键名。如果该键名中在所在的节中不存在,将创建一个键名。如果该参数为NULL,包括节及节下的所有项目都将被删除。
_In_ LPCTSTR lpString, //写入到键值中的数据。
_In_ LPCTSTR lpFileName //INI文件的名称。
);
Parameters
lpAppName [in]
The name of the section to which the string will be copied. If the section does not exist, it is created. The name of the section is case-independent; the string can be any combination of uppercase and lowercase letters.
lpKeyName [in]
The name of the key to be associated with a string. If the key does not exist in the specified section, it is created. If this parameter is NULL, the entire section, including all entries within the section, is deleted.
lpString [in]
A null-terminated string to be written to the file. If this parameter is NULL, the key pointed to by the lpKeyName parameter is deleted.
lpFileName [in]
The name of the initialization file.
If the file was created using Unicode characters, the function writes Unicode characters to the file. Otherwise, the function writes ANSI characters.
Return value
If the function successfully copies the string to the initialization file, the return value is nonzero.
If the function fails, or if it flushes the cached version of the most recently accessed initialization file, the return value is zero. To get extended error information, call GetLastError.
2.GetPrivateProfileString function:用于获取INI文件中的字符串数据。
Retrieves a string from the specified section in an initialization file.
Note This function is provided only for compatibility with 16-bit Windows-based applications. Applications should storeinitialization information in the registry.
Syntax C++
DWORD WINAPI GetPrivateProfileString(
_In_ LPCTSTR lpAppName, //节名。如果该参数为NULL,函数将复制所有的节名到所表示的缓冲区。
_In_ LPCTSTR lpKeyName, //键名。如果该参数为NULL,函数将lpAppName节下的所有键名赋值到lpReturnedString缓冲区中。
_In_ LPCTSTR lpDefault, //默认值
_Out_ LPTSTR lpReturnedString, //用于接收数据的缓冲区。
_In_ DWORD nSize, //以字符为单位表示lpReturnedString缓冲区的大小。
_In_ LPCTSTR lpFileName //文件名称。
);
Parameters
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.
Return value
The return value is the number of characters copied to the buffer, not including the terminating null character.
If neither lpAppName nor lpKeyName is NULL and the supplied destination buffer is too small to hold the requested string, the string
is truncated and followed by a null character, and the return value is equal to nSize minus one.
If either lpAppName or lpKeyName is NULL and the supplied destination buffer is too small to hold all the strings, the last string is
truncated and followed by two null characters. In this case, the return value is equal to nSize minus two.
In the event the initialization file specified by lpFileName is not found, or contains invalid values, this function will set errorno
with a value of '0x2' (File Not Found). To retrieve extended error information, call GetLastError.
3.GetPrivateProfileInt :function用于从INI文件中获取整型数据。
Retrieves an integer associated with a key in the specified section of an initialization file.
Note This function is provided only for compatibility with 16-bit Windows-based applications. Applications should store
initialization information in the registry.
Syntax
C++
UINT WINAPI GetPrivateProfileInt(
_In_ LPCTSTR lpAppName, //节名
_In_ LPCTSTR lpKeyName, //键名
_In_ INT nDefault, //默认值
_In_ LPCTSTR lpFileName //文件名称
);
Parameters
lpAppName [in]
The name of the section in the initialization file.
lpKeyName [in]
The name of the key whose value is to be retrieved. This value is in the form of a string; the GetPrivateProfileInt function converts
the string into an integer and returns the integer.
nDefault [in]
The default value to return if the key name cannot be found in the initialization file.
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.
Return value
The return value is the integer equivalent of the string following the specified key name in the specified initialization file. If the
key is not found, the return value is the specified default value.
4.GetPrivateProfileSectionNames :function用于返回INI文件中的所有节名。
Retrieves the names of all sections in an initialization file.
Note This function is provided only for compatibility with 16-bit Windows-based applications. Applications should store initialization information in the registry.
Syntax C++
DWORD WINAPI GetPrivateProfileSectionNames(
_Out_ LPTSTR lpszReturnBuffer, //接收节名的数据缓冲区
_In_ DWORD nSize, //缓冲区大小
_In_ LPCTSTR lpFileName //INI文件名称
);
Parameters
lpszReturnBuffer [out]
A pointer to a buffer that receives the section names associated with the named file. The buffer is filled with one or more null-terminated strings; the last string is followed by a second null character.
nSize [in]
The size of the buffer pointed to by the lpszReturnBuffer parameter, in characters.
lpFileName [in]
The name of the initialization file. If this parameter is NULL, the function searches the Win.ini file. If this parameter does not contain a full path to the file, the system searches for the file in the Windows directory.
Return value
The return value specifies the number of characters copied to the specified buffer, not including the terminating null character. If the buffer is not large enough to contain all the section names associated with the specified initialization file, the return value is equal to the size specified by nSize minus two.
5.GetPrivateProfileSection :function用于返回指定节下的所有键名与键值。
Retrieves all the keys and values for the specified section of an initialization file.
Note This function is provided only for compatibility with 16-bit applications written for Windows. Applications should store initialization information in the registry.
Syntax C++
DWORD WINAPI GetPrivateProfileSection(
_In_ LPCTSTR lpAppName, //节名
_Out_ LPTSTR lpReturnedString, //接收数据的缓冲区
_In_ DWORD nSize, //缓冲区大小
_In_ LPCTSTR lpFileName //INI文件名称
);
Parameters
lpAppName [in]
The name of the section in the initialization file.
lpReturnedString [out]
A pointer to a buffer that receives the key name and value pairs associated with the named section. The buffer is filled with one or more null-terminated strings; the last string is followed by a second null character.
nSize [in]
The size of the buffer pointed to by the lpReturnedString parameter, in characters. The maximum profile section size is 32,767 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.
Return value
The return value specifies the number of characters copied to the buffer, not including the terminating null character. If the buffer is not large enough to contain all the key name and value pairs associated with the named section, the return value is equal
to nSize minus two.
对于本文刚开始给出的INI文件,给出简单的操作案例,代码如下:
#include "stdafx.h"
#include <iostream>
#include <stdio.h>
#include <windows.h>
using namespace std;
int main(int argc, char* argv[])
{
char UserName_Input[20];
char Password_Input[20];
char UserName[20];
char Password[20];
cout<<"Please input username:";
cin>>UserName_Input;
cout<<"Please input password:";
cin>>Password_Input;
GetPrivateProfileString("UserInfo","UserName","",UserName,20,"./UserInfo.ini");
GetPrivateProfileString("UserInfo","Password","",Password,20,"./UserInfo.ini");
if((strcmp(UserName_Input,UserName)!=0)||(strcmp(Password_Input,Password)!=0))
cout<<"登录失败"<<endl;
else
cout<<"登录成功"<<endl;
char password2[20]; //保存新密码
cout<<"modify the password:";
cin>>password2;
WritePrivateProfileString("UserInfo","Password",password2,"./UserInfo.ini");
system("pause");
return 0;
}