C ++ INI File

Source: Internet
Author: User

C ++ INI File

INI files are mostly used for storage.Program. For example, record the name of the database connecting the program, the name of the last user login, and the user registration information.

I. ini file format

 
[Section1 name] keyname1=Value1keyname2=Value2...... [section2 name] keyname1=Value1keyname2= Value2

Section: node name; keyname: key name; Value: key value. An INI file can have multiple sections, each of which can contain multiple keys.

Ii. Read and Write INI files

1. getprivateprofileint-the integer of the key specified in the specified section of the INI File

Getprivateprofileint (lpctstr lpappname,//Point to the string address containing the section nameLptstr lpkeyname,//Point to the string address containing the key nameInt ndefault//If the key value is not found, the default value is returned.Lptstr lpfilename//INI File Name);

2. getprivateprofilestring-specify the string value of the key from the specified section of the INI File

Getprivateprofilestring (lpctstr lpappname,//Point to the string address containing the section nameLptstr lpkeyname,//Point to the string address containing the key nameLptstr lpdefault,//If the key value is not found, the default string address is returned.Lptstr lpreturnedstring,//Returns the buffer address of the string.DWORD nsize//Buffer LengthLptstr lpfilename//INI File Name);

3. getprivateprofilesection-read the content of the specified section from the INI File

Getprivateprofilesection (lpctstr lpappname,//Point to the string address containing the section nameLptstr lpreturnedstring,//Buffer address of the returned dataDWORD nsize//Buffer length of returned dataLptstr lpfilename//INI File Name);

4. getprivateprofilesectionnames-Get all section information from the INI File

 
Getprivateprofilesectionnames (lptstr lpszreturnbuffer,//Buffer address of the returned dataDWORD nsize//Buffer length of returned dataLptstr lpfilename//INI File Name);

5. writeprivateprofilesection-Write the content of the entire section into the specified section of the INI File

 
Writeprivateprofilesection (lpctstr lpappname,//Point to the string address containing the section nameLptstr lpstring//Address of the data to be writtenLptstr lpfilename//INI File Name);

6. writeprivateprofilestring-Write the string value of the specified key to the specified section of the INI File

Writeprivateprofilestring (lpctstr lpappname,//Point to the string address containing the section nameLptstr lpkeyname,//Point to the string address containing the key nameLptstr lpstring//String address to be writtenLptstr lpfilename//INI File Name);

3. Application

In actual use, getprivateprofilestring and writeprivateprofilestring are used most. However, when operating on a custom INI file, note that if the file specified by lpfilename does not contain path information, by default, the API is the installation directory of Windows instead of the current path of the system. \ XXX. INI indicates that the API operation object is XXX in the current path. INI file.

(1) Save

 Void Cremotemgrdlg: onsave (){  //  Todo: add your control notification handler code here  Cstring ip_save;  Char Inbuf [ 256  ];  Char Path [ 255  ]; M_ctrlip.getwindowtext (ip_save );  If (Strcmp (ip_save, m_ipdata )! = 0 )//  IP address changed  {Getcurrentdirectory (  256  , Inbuf); sprintf (path,  "  % S \ data. ini  " , Inbuf ); //  Set the full path of the INI File  Writeprivateprofilestring (  "  Information  " , " IP address  "  , Ip_save, PATH); strcpy (m_ipdata, ip_save); MessageBox (  "  Saved successfully!  " , "  Prompt  "  );}} 

(2) read and Load

 Cstring cremotemgrdlg: data_loading (){  Char Path [ 255  ];  Char Inbuf [ 256  ]; Cstring ip_save; getcurrentdirectory (  256  , Inbuf); sprintf (path,  "  % S \ data. ini  " , Inbuf ); //  Set the full path of the INI File  Getprivateprofilestring (  "  Information  " , " IP address  " , "" , Ip_save.getbuffer ( 255 ), 255 , PATH ); //  Read data  M_ctrlip.setwindowtext (ip_save); m_ctrlport.setwindowtext (  "  8888  "  ); Strcpy (m_ipdata, ip_save );  Return  Ip_save ;} 

(3) Delete content under the node name

If you save a large amount of data, and the number of data stored each time is not fixed, you may need to delete the content of this node name before saving the data.

 Bool cremotemgrdlg: delsection (maid ){  Char Inbuf [ 256  ];  Char Path [ 255  ]; Getcurrentdirectory (  256  , Inbuf); sprintf (path,  "  % S \ data. ini  "  , Inbuf ); If  (Writeprivateprofilestring (lplayback ction, null, null, PATH ))  Return  False;  Else           Return  Getlasterror ();} 

(4) Writing of array-type data with the same nature

You can write the length of the array first, and then write the array content in the for loop.

Bool cinifile: Write (maid) {char mkeyname [64]; Sprintf (mkeyname,"% S _ % LD", Keyname, rowindex );ReturnWrite (appname, mkeyname, lpstring);} bool cinifile: Write (maid ){ReturnWriteprivateprofilestring (appname, keyname, lpstring, m_filename );}

(4) reading array-type data of the same nature

You can read the length of the array first, and then read the array content in the for loop.

DWORD cinifile: Read (maid, long rowindex, lpstr lpdefault, lpstr returnedstring, DWORD nsize) {char mkeyname [64]; Sprintf (mkeyname,"% S _ % LD", Keyname, rowindex );ReturnRead (appname, mkeyname, lpdefault, returnedstring, nsize);} DWORD cinifile: Read (lpcstr appname, lpcstr keyname, lpcstr lpdefault, lpstr returnedstring, DWORD nsize ){ReturnGetprivateprofilestring (appname, keyname, lpdefault, returnedstring, nsize, m_filename );}

Of course, Data Reading and Writing can also be done using common files.

 

 

 

 

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.