Use getprivateprofilestring to read the configuration file (. INI)

Source: Internet
Author: User
INI files are often used in configuration files. In VC, their functions are:

Write the. ini file: bool writeprivateprofilestrinG (maid, maid );

Read the. ini file: DWORDGetprivateprofilestring(Lptstr lpappname, lptstr lpkeyname, lptstr lpdefaut, lpstr lpreturnedstring, DWORD nsize, lptstr lpfilename );

Read integer value: uint getprivateprofileint (lpappname, lptstr lpkeyname, int ndefault, lptstr lpfilename );

The meanings of the parameters are as follows:

Lptstr lpappname ------- a field name in the INI File

Lptstr lpkeyname -------- a key name under lpappname, that is, the specific variable name in it

Lpctstr lpstring --------- is the key value, that is, the value of the variable. It must be of the type of lpctstr or cstring.

Lpctstr lpfilename -------- complete INI file path name

Lpctstr lpdefaut ---------- if there is no value for the first two parameters, this value is assigned to the variable

Lpstr lpreturnedstring -------- cstring object that receives the value in the INI file, that is, the receiving buffer

DWORD nsize ------ size of the receiving buffer

Example:

Cstring strname, strtemp;

Int Nage;

Strname = "Jacky ";

Nage = 13;

WriteprivateprofilestrinG ("student", "name", strname, "C: \ setting. ini ");

Result: The following figure is displayed in the INI file :)

[Student]

Name = Jacky

Read:

Cstring sname;

Getprivateprofilestring("Student", "name", "defaultname", sname. getbuffer (max_length), max_length, "C: \ setting. ini ");

Result: sname = "Jacky". Note that the function must be released after the getbuffer function is used up (sname is used. releasebuffer () function), otherwise other subfunctions that use sname will fail.

Reading integers is relatively simple, as shown below:

Int result = getprivateprofileint ("student", "Nage", 0, "C: \ setting. ini") the returned value is the read result!

InGetprivateprofilestringThe last parameter is the configuration file path parameter, which can only be an absolute path, not a relative path, but now I need to be able to include my EXE file with my configuration file. So I used the getcurrentdirectory function.

OriginalCodeAs follows:

Cstring server_ip;
Cstring des = "";
: Getcurrentdirectory (max_pathlength, Des. getbuffer (max_pathlength ));
Des. releasebuffer ();
Des + = "\ config. ini ";
Getprivateprofilestring("Phonedemo", "server_ip", "", server_ip.getbuffersetlength (15), 15, des );
Server_ip.releasebuffer ();

Note: when using the cstring variable here, after using the getbuffer function, you must use the releasebuffer () function to perform other operations such as string +.

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.