Use of vc ini file read/write and getprofilestring and writeprofilestring Functions

Source: Internet
Author: User
Tags delete key read ini file

 

How to read and write INI files using functions in VC

INI file (initialization file), which usually stores the initialization information of a program. An INI file consists of several sections. Each section consists of several keys, and each key can be assigned a corresponding value. Reading and writing an INI file is actually the value of the corresponding key in a section, which can be completed by using several functions.

[section]key=string      .      .      .

I. Functions for writing information to the INI File
1. Write information to the win. ini file of the system.
Bool writeprofilestring (
Lptstr lpappname, // segment name, is a string ending with 0
Lptstr lpkeyname, // key name, is a string ending with 0. If it is null, the entire section is deleted.
The value of the lpstring // key, which is a string ending with 0. If it is null, the corresponding key is deleted.
)

2. Write the information to the custom. ini file.
Bool writeprivateprofilestring (
Lptstr lpappname, // same as above
Lptstr lpkeyname, // same as above
Lptstr lpstring, // same as above
Lptstr lpfilename // file name of the file to be written. If the INI file is in the same directory as the program, you can also use
// Path. Otherwise, the absolute path must be provided.
)

For example:
: Writeprofilestring ("test", "ID", "xym ");
// Create a test section in win. ini and a key ID in this section. Its value is xym.

: Writeprivateprofilestring ("test", "ID", "xym", "d: // VC // ex1 // ex1.ini ");
// Create a test section in ex1.ini under the ex1 directory, and create a key ID in this section. Its value is xym.

// If the ex1.ini file is in the same directory as the program that reads and writes the file, the preceding statement can also be written as follows:
: Writeprivateprofilestring ("test", "ID", "xym", ". // ex1.ini ");

Note that the escape character '//' in the C series indicates the backslash '/'. In addition, when the relative path is used, the '.' before '//' cannot be lost.

2. functions used to read data from the INI File
1. Read Information from the win. ini file of the system
(1) Reading strings
DWORD getprofilestring (
Lptstr lpappname, // node name
Lptstr lpkeyname, // key name, reads the value of this key
Lptstr lpdefault, // if the specified key does not exist, this value is the default value for reading.
Lptstr lpreturnedstring, // a pointer to the buffer to receive the read string
DWORD nsize // specify the buffer size pointed to by lpreturnedstring
)

For example:
Cstring STR;
: Getprofilestring ("test", "ID", "error", str. getbuffer (20), 20 );

(2) read Integers
Uint getprofileint (
Lptstr lpappname, // same as above
Lptstr lpkeyname, // same as above
Int ndefault // if the specified key name does not exist, this value is the default value for reading.
)

Use the following statement to write age information:
: Writeprofilestring ("test", "Age", "25 ");
// Create a test section in win. ini and create a key age in this section. The value is 25.

You can use the following statement to read the value of the age key:
Int age;
Age =: getprofileint ("test", "Age", 0 );

2. read information from your INI File
(1) Reading strings
DWORD getprivateprofilestring (
Lptstr lpappname, // same as 1 (1)
Lptstr lpkeyname, // same as 1 (1)
Lptstr lpdefault, // same as 1 (1)
Lptstr lpreturnedstring, // same as 1 (1)
DWORD nsize, // same as 1 (1)
Lptstr lpfilename // The name of the file to read the information. If the INI file is in the same directory as the program
// Specifies the path. Otherwise, the absolute path must be provided.
)

For example:
Cstring STR;
: Getprivateprofilestring ("test", "ID", "error", str. getbuffer (20), 20, ". // ex1.ini ");
Or:
: Getprivateprofilestring ("test", "ID", "error", str. getbuffer (20), 20, "d: // VC // ex1 // ex1.ini ");

(2) read Integers

Uint getprivateprofileint (
Lptstr lpappname, // same as above
Lptstr lpkeyname, // same as above
Int ndefault, // if the specified key name does not exist, this value is the default value for reading.
Lptstr lpfilename // same as above
)

Use the following statement to write age information:
: Writeprivateprofilestring ("test", "Age", "25", ". // ex1.ini ");
// Create a test section in ex1.ini and a key age in this section. The value is 25.

You can use the following statement to read the value of the age key:
Int age;
Age =: getprivateprofileint ("test", "Age", 0, ". // ex1.ini ");

3. delete key values or segments

Let's review the description of the writeprofilestring function.
Bool writeprofilestring (
Lptstr lpappname, // segment name, is a string ending with 0
Lptstr lpkeyname, // key name, is a string ending with 0. If it is null, the entire section is deleted.
The value of the lpstring // key, which is a string ending with 0. If it is null, the corresponding key is deleted.
)

To delete a section, you only need to set the second parameter writeprofilestring to null. To delete a key, you only need to set the third parameter of the function to null. This is to delete the section or key in win. ini of the system. Similarly, to delete the section or key in the INI file you have defined, you can also perform the same operation.
For example:
: Writeprofilestring ("test", null, null); // Delete the test section in win. ini
: Writeprofilestring ("test", "ID", null); // Delete the ID key in win. ini

: Writeprivateprofilestring ("test", null, null, ". // ex1.ini"); // Delete the test section in ex1.ini
: Writeprivateprofilestring ("test", "ID", null, ". // ex1.ini"); // Delete the ID key in ex1.ini

4. How to determine the number of segments in an INI File
To determine the number of segments in an INI file, the simplest way is to find out all the segments and then count the number of segments. To find out all the node names, use the getprivateprofilesectionnames function. Its prototype is as follows:
DWORD getprivateprofilesectionnames (
Lptstr lpszreturnbuffer, // point to a buffer to save all the returned node names
DWORD nsize, // the size of the lpszreturnbuffer Parameter
Lptstr lpfilename // file name. If the INI file is in the same directory as the program,

// You can also use relative paths. Otherwise, you must provide absolute paths.
)

The following is a function used to count the total number of sections in an INI file. Of course, if you need to find the keys and their values in each section at the same time, you can easily find the section name.

/* Count the total number of sections
Separation of node names: if the first character of the chsectionnames array is '/0', it indicates
There are 0 sections. Otherwise, start from the first character of the chsectionnames array and search for it in sequence,
Until a '/0' character is found. If the successor character of this character is not'/0', it indicates the previous
The preceding characters form a node name. If two '/0' characters are found consecutively, the statistics ends */

Int ctestdlg: calccount (void)
{
Tchar chsectionnames [2048] = {0}; // character array composed of all node names
Char * psectionname; // Save the first address of a specified node name string
Int I; // I points to a location of the array chsectionnames, starting from 0 and moving backward
Int J = 0; // J is used to save the offset of the first address of the next node name string relative to the current I.
Int COUNT = 0; // count the number of nodes.

// Cstring name;
// Char ID [20];
: Getprivateprofilesectionnames (chsectionnames, 2048, ". // ex1.ini ");
For (I = 0; I <2048; I ++, J ++)
{
If (chsectionnames [0] = '/0 ')
Break; // if the first character is 0, it indicates that there is no section in ini.
If (chsectionnames [I] = '/0 ')
{
Psectionname = & chsectionnames [I-j]; // If a value of 0 is found, it indicates that J offsets are removed from the forward of the character,
// It is the first address of a node name.

J =-1; // after a node name is found, the value of J is restored to count the offset of the next node name address.
//-1 is assigned because the last character 0 of the node name string is the Terminator and cannot be used as the node name.

// Part
/*: Getprivateprofilestring (psectionname, "ID", "error", ID, 20, ". // ex1.ini ");
Name. Format ("% s", ID );*/
// You can obtain the value of the key in the section when obtaining the node name, provided that we know which keys are in the Section.

Afxmessagebox (ctionctionname); // display the found

If (chsectionnames [I + 1] = 0)
{
Break; // when both adjacent characters are 0, all the node names are found and the cycle ends.
}
}
}
Return count;
}/////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// ///////////

//////////////////////////////////////// /////////////////////////

In the VC program, use the getprivateprofilestring and writeprivateprofilestring functions provided by the system to directly read and write the System Configuration INI file (the INI file in the specified directory)

Assume that there is a file named TETS. ini in the current directory.
Used to save the user name and password
The file format is as follows:
[Section1]
Item1 = huzhifeng
Item2 = 1234565

1. Write the INI File
Void cini_file_testdlg: onbuttonwrite ()
{
// Todo: add your control notification handler code here

Cstring strsection = "Section1 ";
Cstring strsectionkey = "Item1 ";
Char strbuff [256];
Cstring strvalue = _ T ("");
Cstring strfilepath;

Strfilepath = getcurrentdirectory (256, strbuff); // obtain the current path
Strfilepath. Format ("% S // test. ini", strbuff );

Getdlgitemtext (idc_edit_name, strvalue); // get the text box content: Name
Writeprivateprofilestring (strsection, strsectionkey, strvalue, strfilepath); // write the corresponding fields in the INI File

Strsectionkey = "item2 ";
Getdlgitemtext (idc_edit_password, strvalue); // obtain the text box content, that is, the password.
Writeprivateprofilestring (strsection, strsectionkey, strvalue, strfilepath );
}

2. Read INI File Content
Void cini_file_testdlg: onbuttonread ()
{
// Todo: add your control notification handler code here
Cstring strsection = "Section1 ";
Cstring strsectionkey = "Item1 ";
Char strbuff [256];
Cstring strvalue = _ T ("");
Cstring strfilepath;

Strfilepath = getcurrentdirectory (256, strbuff); // obtain the current path
Strfilepath. Format ("% S // test. ini", strbuff );

Getprivateprofilestring (strsection, strsectionkey, null, strbuff, 80, strfilepath); // read the content of the corresponding field in the INI File
Strvalue = strbuff;
Setdlgitemtext (idc_edit_name, strvalue );

Strsectionkey = "item2 ";
Getprivateprofilestring (strsection, strsectionkey, null, strbuff, 80, strfilepath );
Strvalue = strbuff;
Setdlgitemtext (idc_edit_password, strvalue );

Updatedata (false );
}

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.