How to read and write INI configuration files (summary)

Source: Internet
Author: User

We may often use the configuration file INI file to obtain or save the parameter information, in the VC its functions are mainly used in:
    • Read
  1. Read characters

    DWORD GetPrivateProfileString (
    LPCTSTR
    lpappname, //INI file a field name [section name] can have a number of section names

    LPCTSTR lpkeyname, //Lpappname A key name, that is, the specific variable name inside

    LPCTSTR lpdefault, //If lpreturnedstring is empty, assign a variable to lpreturnedstring

    LPTSTR lpreturnedstring, //pointer variable holding key value, receive buffer for receiving key values (data) in INI file

    DWORD nSize, //lpreturnedstring buffer size

    LPCTSTR path to lpFileName//INI file
    );

  2. Read integer value (the return value is read to the whole)

    UINT Getprivateprofileint (

    LPCTSTR lpappname, //INI file a field name [section name] can have a number of section names
    LPCTSTR lpkeyname, //Lpappname A key name, that is, the specific variable name inside
    INT ndefault, //If the specified data return is not found, assign a variable value to the return value

    LPCTSTR path to lpFileName//INI file

    );

    • Write
    1. Write characters

      BOOL WritePrivateProfileString (
      LPCTSTR
      lpappname, //INI file a field name [section name] can have a number of section names

      LPCTSTR lpkeyname, //Lpappname A key name, that is, the specific variable name inside

      LPCTSTR lpstring, //key value, i.e. data

      LPCTSTR path to lpFileName//INI file
      );

    2. Write integral type
    • Read and write INI file relative path and absolute path can be, according to the actual situation to choose

".. \\IniFileName.ini "//This is a relative path

"D:\\inifilename.ini"//such as absolute path

    • MAX_PATH: is the macro set by Microsoft's largest path-occupied byte

  The INI file written is divided into: section, key name, key value

Give me a chestnut:

Xx.ini

[Serial port configuration]

Baud Rate =19200

With the theory, take a look at the demo of Practice:

    • Write INI file:  

LPTSTR Lppath = new Char[max_path];

strcpy (Lppath, "D:\\inifilename.ini");

WritePrivateProfileString ("Liming", "Sex", "Man", Lppath);
WritePrivateProfileString ("Liming", "Age", "a", "Lppath");

WritePrivateProfileString ("Fangfang", "Sex", "Woman", Lppath);
WritePrivateProfileString ("Fangfang", "Age", "$", Lppath);

delete [] lppath;

INI file is as follows:

[Liming]
Sex=man
Age=20
[Fangfang]
Sex=woman
Age=21

    • Read INI file:

LPTSTR Lppath = new Char[max_path];
LPTSTR limingsex = new Char[6];
int limingage;
LPTSTR fangfangsex = new Char[6];
int fangfangage;

strcpy (Lppath, "... \\IniFileName.ini ");

GetPrivateProfileString ("Liming", "Sex", "" ", Limingsex, 6, Lppath);
Limingage = Getprivateprofileint ("Liming", "age", 0, Lppath);

GetPrivateProfileString ("Fangfang", "Sex", "" ", Fangfangsex, 6, Lppath);
Fangfangage = Getprivateprofileint ("Fangfang", "age", 0, Lppath);

delete [] lppath;

How to read and write INI configuration files (summary)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.