VC:INI文件中键名数据的读写(GetPrivateProfileString())

//先选择Project|setting命令下Project setting命令下对话框,在Microsoft Foundation Classes下选择 Use MFC in a Shared DLL

#include<iostream.h>

#include<afx.h>

//#include<afx1.h>

#define MAX_ALLSECTIONS 300  //定义最大的段长度

#define MAX_SECTION 20     //段的最大长度

void main()

{

       //1、键名数据的读写

/*    1

              UINT GetProfileInt(

              LPCTSTR lpszSection,

              LPCTSTR lpszEntry,

              int nDefault );

       2

       UINT GetPrivateProfileInt(

       LPCTSTR lpAppName,  // section name

       LPCTSTR lpKeyName,  // key name

       INT nDefault,       // return value if key name not found

       LPCTSTR lpFileName  // initialization file name

       );

       3

              DWORD GetPrivateProfileString(

                LPCTSTR lpAppName,        // section name

                LPCTSTR lpKeyName,        // key name

                LPCTSTR lpDefault,        // default string

                LPTSTR lpReturnedString,  // destination buffer

                DWORD nSize,              // size of destination buffer

                LPCTSTR lpFileName        // initialization file name

              );

       4

              BOOL WritePrivateProfileString(

                LPCTSTR lpAppName,  // section name

                LPCTSTR lpKeyName,  // key name

                LPCTSTR lpString,   // string to add

                LPCTSTR lpFileName  // initialization file

              );

*/

       LPCTSTR lpFileName="C:\\test.ini";

       CString str;

       char c[10];

       WritePrivateProfileString("SECTION_1","Key1","test1",lpFileName);

       WritePrivateProfileString("SECTION_2","Key2","test2",lpFileName);

       GetPrivateProfileString("SECTION_1","Key1","error",c,10,lpFileName);

       str.Format("%s",c);

       cout<<str<<endl;

}

原文地址:https://www.cnblogs.com/shenchao/p/2737759.html