Read and Write ini configuration files in VC

Source: Internet
Author: User

INI files are often used in configuration files. In VC, their functions are:
Write the. ini file: bool writeprivateprofilestring (lpappname, lptstr lpkeyname, lptstr lpstring, lptstr lpfilename );
Read the. ini file: DWORD getprivateprofilestring (lpappname, lptstr lpkeyname, lptstr lpdefaut, lpstr lpreturnedstring, DWORD nsize, lptlpstr filename );
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!
The last parameter in getprivateprofilestring is the configuration file path parameter, which can only be an absolute path, not a relative path, but now I need my EXE file to be associated with my configuration file. So I used the getcurrentdirectory function.
Original Code As 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 +.
Switch from csdn-cherryt

Bytes ----------------------------------------------------------------------------------------------------------------
VC reads the configuration file (. INI ):
Cstring strip = _ T ("");
Cstring strport = _ T ("");
: Getprivateprofilestring ("TCP/IP", "ip ","",
Strip. getbuffer (max_path), max_path, ". \ config. ini ");
Strip. releasebuffer ();
M_strip = strip; // ip address
: Getprivateprofilestring ("TCP/IP", "Port ","",
Strport. getbuffer (max_path), max_path, ". \ config. ini ");
Strport. releasebuffer ();
M_iport = atoi (strport); // port number

Configuration File Name: config. ini
File structure:
[TCP/IP]
IP = 127.0.0.1
Port = 8848

 

From: http://hi.baidu.com/rdhome/blog/item/9e13d190376dff8ba977a4ec.html

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.