C ++ reads the INI File

Source: Internet
Author: User

For this purpose, the Windows operating system provides six API functions to read and write configuration files:

Getprivateprofileint () obtains integer values from the private initialization file.
Getprivateprofilestring () Get the string value from the private Initialization File
Getprofileint obtains the integer from win. ini.
Getprofilestring get the string value from win. ini
WriteprivateprofilestrinG. Write the string to the private initialization file.
Writeprofilestring write string to win. ini

We can use the oninitialupdate () function of the View classProgramThe entry for reading the configuration file at startup. The storage format of the configuration file is as follows:

[Section 1]
X Pos = 300
Ypos = 200

[Section 2]
TEXT = Hello

There are only two sections. xpos and ypos indicate the coordinates of the information to be displayed, and the information to be displayed is stored in the text item in section 2, read the API functions used to access the private configuration file into the m_nxpos, m_nypos, and m_strtext variables, and call the ondraw () function through invalidate, use the textout function to display the information at the read Coordinate Position:

M_nxpos = getprivateprofileint ("section 1", // node name
"Xpos", // Item Name
0, // The default return value when this item is not found
"C: \ test \ debug \ test. ini"); // the correct path of the configuration file

M_nypos = getprivateprofileint ("section 1", "ypos", 0, exefullpath );
Char Buf [256];
Len = getprivateprofilestring ("section 2", // node name
"Text", // Item Name
"No text", // the return value when this item is not found
Buf, // target buffer address
256, // target buffer Length
"C: \ test \ debug \ test. ini"); // the correct path of the configuration file

For (INT I = 0; I <Len; I ++)
{
Cstring STR;
Str. Format ("% C", Buf [I]);
M_strtext + = STR;
}
Invalidate ();

Generally, the configuration file is stored in the same directory as the application. If "C: \ test \ debug \ test. if you set the absolute path of INI, the configuration file cannot be found after the path is changed. Therefore, you should dynamically search for the storage address of the configuration file:

Tchar exefullpath [max_path]; // max_path is defined in the API, Which is 128
Int Len = getmodulefilename (null,
Exefullpath, // full path storage address of the application
Max_path );
Cstring Path = "\ test. ini"; // configuration file name
: Strcpy (exefullpath + len-13, PATH); // combines the full path of the configuration file

It is worth noting that 13 here is the size of the project name, but different projects may have different names. The length defined here is too mechanical.

1 Char   * P = NULL;
2 Char Exefullpath [ 128 ];
3 Int Len = Getmodulefilename (null,
4 Exefullpath, 128 );
5 P = Strrchr (exefullpath, ' \\ ' );
6 * P = ' \ 0 ' ;

In this way, the strrchr function can block the final '\' and block the project name. There are also different practices based on different situations.

Writing a configuration file is similar. You only need to format the variable of the numeric type into a string and store it again:

Str. Format ("% d", m_nxpos );
WriteprivateprofilestrinG ("section 1", "xpos", STR, exefullpath );
Str. Format ("% d", m_nypos );
WriteprivateprofilestrinG ("section 1", "ypos", STR, exefullpath );
WriteprivateprofilestrinG ("section 2", "text", m_strtext, exefullpath );

We must have encountered such a program: After executing the program again, the system will automatically load the program after restarting. In fact, in addition to adding information in the Startup menu and registry, writeprofilestring () can also be used () function to win. the "run" project in the "Windows" section of INI adds the full path of the application, which is much easier and safer than the other two methods.

2. Read the information from the INI file into the variables in the program.

1. the prototype of the winapi function used is:

DWORD getprivateprofilestring (
Lptstr lpappname,
Lptstr lpkeyname,
Lptstr lpdefault,
Lptstr lpreturnedstring,
DWORD nsize,
Lptstr lpfilename
);

Meanings of parameters:

The first two parameters and writeprivateprofilestrinG has the same meaning.

Lpdefault: If the INI file does not contain the field name or key name specified by the first two parameters, this value is assigned to the variable.

Lpreturnedstring: The cstring object that receives the value in the INI file, that is, the destination cache.

Nsize: the size of the destination cache.

Lpfilename: the complete INI file name.

2. Usage: Read the student information written in the previous step into the program.

Cstring strstudname;
Int nstudage;
Getprivateprofilestring ("studentinfo", "name", "default name", strstudname. getbuffer (max_path), max_path, "C: \ stud \ student. ini ");

After execution, the value of strstudname is "Zhang San". If the first two parameters are incorrect, the value is "default name ".

3. Use another winapi function to read integer values:

Uint getprivateprofileint (
Lptstr lpappname,
Lptstr lpkeyname,
Int ndefault,
Lptstr lpfilename
);

The parameters here have the same meaning as above. The usage is as follows:
Nstudage = getprivateprofileint ("studentinfo", "Age", 10, "C: \ stud \ student. ini ");

Add an example of self-testing Win32 and passing the test (partialCodeThe main function is to configure the relative path, which will be available later)

1 Char   * P = NULL;
2 Char Exefullpath [ 128 ];
3 Int Len = Getmodulefilename (null,
4 Exefullpath, // Full path storage address of the application
5 128 );
6 P = Strrchr (exefullpath, ' \\ ' ); // Blocked project name
7 * P = ' \ 0 ' ;
8 P = Strrchr (exefullpath, ' \\ ' ); // Block debug (this may not be required in actual development)
9 * P = ' \ 0 ' ;
10 Len = Strlen (exefullpath );
11 String Path = " \ System. ini " ; // Configuration File Name
12 : Strcpy (exefullpath + Len, path. c_str ()); // Combine the full path of the configuration file
13
14
15 Char Ipstr [ 20 ]; // Storage IP Address
16 Getprivateprofilestring ( " Server " , " Serverip " , Null, ipstr, 20 , Exefullpath );
17 Int Port;
18 Port = Getprivateprofileint ( " Server " , " Port " , 0 , Exefullpath );

The following section is written during work in the company. make a record.

1 //////////////////////////////////////// //Separate content ','
2 String Strfream = Szfream;
3 Vector < String > Strvec;
4 Char Ctrim =   ' , ' ;
5 STD :: String : Size_type pos1, pos2;
6 Pos2 =   0 ;
7 While (Pos2 ! = STD :: String : NPOs)
8 {
9 Pos1 = Strfream. find_first_not_of (ctrim, pos2 );
10 If (Pos1 = STD :: String : NPOs)
11 Break ;
12 Pos2 = Strfream. find_first_of (ctrim, pos1 +   1 );
13 If (Pos2 = STD :: String : NPOs)
14 {
15If(Pos1! =Strfream. Size ())
16Strvec. push_back (strfream. substr (pos1 ));
17Break;
18}
19 Strvec. push_back (strfream. substr (pos1, pos2 - Pos1 ));
20 }
21 For ( Int I =   0 ; I < Strvec. Size (); ++ I)
22 {< br> 23 int ntemp = atoi (strvec [I]. c_str ());
24 If (ntemp m_nframenum)
25 m_vecframe.push_back (ntemp );
26 else continue ;
27 }
Related Article

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.