头文件
Windows.h
举个例子,配置文件set.cfg内容:
[testdata]
UserName=600511005
Password=112233
ResetSeqNumFlag=Y
InputType=Z
ClOrdID =9893
[section]
key=string
[Student]
Name=jacky
函数GetPrivateProfileInt可以读取配置文件中的数字,
UINT WINAPI GetPrivateProfileInt(
_In_ LPCTSTR lpAppName,
_In_ LPCTSTR lpKeyName,
_In_ INT nDefault,
_In_ LPCTSTR lpFileName
);如
UINT dwClOrdID = GetPrivateProfileInt("testdata","ClOrdID",0, "./set.cfg");
读取字符串稍微复杂,如
同样,可以利用函数WritePrivateProfileString将配置写到文件中,
写配置所用的代码如下:
WritePrivateProfileString (TEXT("Section1"),
TEXT("FirstKey"),
TEXT("It all worked out OK."),
TEXT("appname.ini"));
WritePrivateProfileString (TEXT("Section1"),
TEXT("SecondKey"),
TEXT("By golly, it works!"),
TEXT("appname.ini"));
WritePrivateProfileString (TEXT("Section1"),
TEXT("ThirdKey"),
TEXT("Another test..."),
TEXT("appname.ini"));
// Test
GetPrivateProfileString (TEXT("Section1"),
TEXT("FirstKey"),
TEXT("Error: GPPS failed"),
inBuf,
80,
TEXT("appname.ini"));
_tprintf (TEXT("Key: %s\n"), inBuf);
Windows配置文件操作
本文介绍如何使用Windows API函数GetPrivateProfileInt和WritePrivateProfileString来读取和写入INI配置文件。通过具体示例展示了如何从配置文件中读取整数和字符串值,并将新的配置项写入文件。
73

被折叠的 条评论
为什么被折叠?



