C + + Read INI file

Source: Internet
Author: User
Tags configuration settings read ini file

The Windows operating system specifically provides 6 API functions for reading and writing configuration settings files:

Getprivateprofileint () Gets the integer value from the private initialization file
GetPrivateProfileString () Gets the string value from the private initialization file
Getprofileint getting integer values from Win.ini
GetProfileString getting string values from Win.ini
WritePrivateProfileString writing a string to a private initialization file
WriteProfileString write String to Win.ini

We can take the view class's: OnInitialUpdate () function as a portal to read the configuration file when the program starts, and the configuration file is stored in the following format:

[Section 1]
xpos=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 the second section, which is read into the variables m_nxpos,m_nypos and M_strtext, respectively, by reading the API functions that access the private configuration settings file. The OnDraw () function is called through invalidate (), where the information is displayed in the coordinates where it is read using the TextOut function:

M_nxpos=getprivateprofileint ("section 1",//Stanza name
"XPos",//Item name
0,//The default return value when this item is not found
"C:\test\debug\test.ini"); Exact path to the configuration file

M_nypos=getprivateprofileint ("section 1", "YPos", 0,exefullpath);
Char buf[256];
Len=getprivateprofilestring ("section 2",//Stanza name
"Text",//Item name
"No Text",//return value when this item is not found
BUF,//destination buffer address
256,//target buffer length
"C:\test\debug\test.ini"); Exact path to the configuration file

for (int i=0;i<len;i++)
{
CString str;
Str. Format ("%c", Buf[i]);
M_STRTEXT+=STR;
}
Invalidate ();

General configuration files are stored in the same directory as the application if the "C:\test\debug\test.ini" absolute path is set to the path can not find the configuration file after the problem, you should dynamically search for the configuration file storage address:

Tchar Exefullpath[max_path]; MAX_PATH is defined in the API and is 128
int Len=getmodulefilename (NULL,
Exefullpath,//full path storage address for the application
MAX_PATH);
CString path= "\test.ini"; Profile Name
:: strcpy (Exefullpath+len-13,path); To combine the full path of a configuration file

It is worth noting that the 13 here is the size of the project name, but different items may not have the same name, so the length of the definition is too mechanized

1 char *p = NULL;
2 Char exefullpath[128];
3 int Len=getmodulefilename (NULL,
4 Exefullpath, 128);
5 P=STRRCHR (exefullpath, ' \ \ ');
6 *p= ' + ';

In this way, through the STRRCHR function to shield off the last appearance of the "\" will be able to block the project name, depending on the circumstances of course there are different practices.


Write configuration files are basically similar, just need to format variables of numeric type into string and then store:

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 the execution of the system will automatically load the program, in addition to the boot menu and the registry to add information, you can also use the writeprofilestring () function to Win.ini "Windows" section of "Run" The project adds the full path to the application, which is much simpler and more secure than the other two methods.



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

1. The WINAPI function prototype used is:

DWORD GetPrivateProfileString (
LPCTSTR Lpappname,
LPCTSTR Lpkeyname,
LPCTSTR Lpdefault,
LPTSTR lpreturnedstring,
DWORD NSize,
LPCTSTR lpFileName
);

The meaning of each parameter:

The first two parameters are the same as in WritePrivateProfileString.

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

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

NSize: The size of the destination buffer.

lpFileName: Is the full INI file name.

2. How to use: The student's information written in the previous step is now read into the program.

CString Strstudname;
int nstudage;
GetPrivateProfileString ("Studentinfo", "name", "Default name", Strstudname.getbuffer (MAX_PATH), MAX_PATH, "c:\stud\ Student.ini ");

The value of Strstudname after execution is: "Zhang San", if the first two parameters are incorrect, the value is "default name".

3. Read-in integer value to use another WINAPI function:

UINT Getprivateprofileint (
LPCTSTR Lpappname,
LPCTSTR Lpkeyname,
INT Ndefault,
LPCTSTR lpFileName
);

The parameters here have the same meaning as above. Use the following method:
Nstudage=getprivateprofileint ("Studentinfo", "age", Ten, "C:\stud\student.ini");




Paste your own WIN32 test and pass an example (part of the code, the main function is how to configure the relative path, follow-up, the previous already have)

1 char *p = NULL;
2 Char exefullpath[128];
3 int Len=getmodulefilename (NULL,
4 Exefullpath,//full path storage address for application
5 128);
6 P=STRRCHR (exefullpath, ' \ \ '); Block out project name
7 *p= ' + ';
8 P=STRRCHR (exefullpath, ' \ \ '); Block Debug (This may not be required in the actual development)
9 *p= ' + ';
Ten len = strlen (Exefullpath);
One string path= "\\system.ini"; Profile Name
: strcpy (Exefullpath+len,path.c_str ()); To combine the full path of a configuration file
13
14
(+ char ipstr[20]; Storage IP Address
GetPrivateProfileString ("Server", "ServerIP", Null,ipstr,20,exefullpath);
+ int port;
Port = Getprivateprofileint ("Server", "Port", 0,exefullpath);



This is what the company wrote at work, a record.

1Separate the content with ', '
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);
if (pos1 = = Std::string::npos)
Break one by one;
Pos2 = strfream.find_first_of (Ctrim, POS1 + 1);
if (Pos2 = = Std::string::npos)
14{
if (pos1! = Strfream.size ())
Strvec.push_back (Strfream.substr (POS1));
break;
18}
Strvec.push_back (Strfream.substr (POS1, pos2-pos1));
20}
for (int i = 0; i < strvec.size (); ++i)
22{
ntemp int = atoi (Strvec[i].c_str ());
if (Ntemp < M_nframenum)
M_vecframe.push_back (NTEMP);
Continue else;
+}

C + + Read INI file

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.