C++读取、写入配置文件

#include
#include
#include

using namespace std;

#pragma warning(disable:4996)

int main() {

    //写入配置文件
    LPTSTR lpPath = new char[MAX_PATH];

    strcpy(lpPath, "C://test.ini");

    WritePrivateProfileString("LiMing", "Sex", "1111", lpPath);
    WritePrivateProfileString("LiMing", "Age", "2222", lpPath);

    WritePrivateProfileString("Fangfang", "Sex", "3333", lpPath);
    WritePrivateProfileString("Fangfang", "Age", "4444", lpPath);

    //读取配置文件
    LPTSTR LiMingSex = new char[6];
    int LiMingAge;
    LPTSTR FangfangSex = new char[6];
    int FangfangAge;


    strcpy(lpPath, "C://test.ini");

    GetPrivateProfileString("LiMing", "Sex", "", LiMingSex, 6, lpPath);
    LiMingAge = GetPrivateProfileInt("LiMing", "Age", 0, lpPath);
    cout << LiMingAge << endl;

    GetPrivateProfileString("Fangfang", "Sex", "", FangfangSex, 6, lpPath);
    FangfangAge = GetPrivateProfileInt("Fangfang", "Age", 0, lpPath);
    cout << FangfangAge << endl;

    delete[] lpPath;

    system("pause"); //用于防止闪退 

}

 

配置文件格式: 
[LiMing] 
Sex=Man 
Age=20 
[Fangfang] 
Sex=Woman 
Age=21

你可能感兴趣的:(C++读取、写入配置文件)