How to read and write INI files using functions in VC

Source: Internet
Author: User
Tags delete key

From: http://blog.csdn.net/kenny9931/archive/2007/06/18/1656805.aspx

 

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.
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) <br/>{< br/> tchar chsectionnames [2048] = {0 }; // character array composed of all node names <br/> char * psectionname; // Save the first address of a node name string found <br/> int I; // I points to a location of the array chsectionnames, starting from 0 and moving backward in sequence <br/> 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 position <br/> int COUNT = 0; // number of statistical sections </P> <p> // cstring name; <br/> // char ID [20]; <br/>:: getprivateprofilesectionnames (chsectionnames, 2048 ,". // ex1.ini "); <br/> for (I = 0; I <2048; I ++, J ++) <br />{< Br/> If (chsectionnames [0] = '/0') <br/> break; // if the first character is 0, if (chsectionnames [I] = '/0 ') <br/> {<br/> psectionname = & chsectionnames [I-j]; // If a value of 0 is found, the offset of J is removed from the forward of the character, <br/> // It is the first address of a node name </P> <p> J =-1; // after a node name is found, the value of J is restored, calculate the offset of the next node name address <br/> // assign-1 because the last character 0 of the node name string is the Terminator, cannot be part of the node name </P> <p> // <br/>/*: getprivateprofilestring (psectionname, "ID", "error", ID, 20 ,". // ex1.ini "); <br/> name. Format ("% s", ID); */<br/> // you can obtain the value of the middle key when obtaining the node name, the premise is that we know which keys are in this section. </P> <p> afxmessagebox (psectionname); // display the found information. </P> <p> If (chsectionnames [I + 1] = 0) <br/>{< br/> break; // when both adjacent characters are 0, all the section names are found, cyclic termination <br/>}</P> <p> return count; <br/>}< br/>

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.