配追文件即ini后缀名的文件,NI 文件存储为文本格式,以键值对的形式表示,每个键值对占据一行
[User]
Name=John
Age=30
Name、Age 则是具体的键,等号后面的部分是相应的值。在程序启动时,程序可以读取对应的 INI 文件,获取其中的配置信息,并根据需要进行相应的处理
代码
写入文件
char szPath[MAX_PATH] = { 0 };
GetCurrentDirectory(MAX_PATH, szPath);
CString szpathfile;
szpathfile.Format("%s\\Test.ini", szPath);
第二个函数表示得到当前文件路径
WritePrivateProfileString("User", "Name", "John", szpathfile);
写入内容,第一个参数表示类别,第二个、三个分别是键值对。第四个表示路径
读取文件
得到路径
char szPath[MAX_PATH] = { 0 };
GetCurrentDirectory(MAX_PATH, szPath);
szpathfile.Format("%s\\Test.ini", szPath);
char string1[100]{};
char string2[100]{};
GetPrivateProfileString("User", "Name", NULL, string1,100,szpathfile);
GetPrivateProfileString("User", "Age", NULL, string2, 100, szpathfile);
stringshow.Format("Name=%s,Age=%s", string1, string2);
MessageBox(stringshow);