VC reads and writes ini files

Source: Internet
Author: User

We wroteProgramThere is always some configuration information to be saved to complete the functions of the program. The simplest way is to write the information into the INI file and then read the information during program initialization. The specific application is as follows:

1. Write information to the. ini file.

1. the prototype of the winapi function used is:

Bool writeprivateprofilestring (
Lptstr lpappname,
Lptstr lpkeyname,
Lptstr lpstring,
Lptstr lpfilename
);

Meanings of parameters:

Lptstr lpappname is a field name in the INI file.

Lptstr lpkeyname is a key name under lpappname. Generally, it is a variable name.

Lpstring is the key value, that is, the value of the variable. However, the value must be of the lpctstr or cstring type.

Lptstr lpfilename is the complete INI file name.

2. Specific usage: If you have a student, you need to write his name and age to the C:/stud/student. ini file.

Cstring strname, strtemp;
Int Nage;
Strname = "James ";
Nage = 12;
: Writeprivateprofilestring ("studentinfo", "name", strname, "C: // stud // student. ini ");

The content in the C:/stud/student. ini file is as follows:

[Studentinfo]

3. To save the student's age, you only need to change the integer value to the balanced type:

Strtemp. Format ("% d", Nage );
: Writeprivateprofilestring ("studentinfo", "Age", strtemp, "C: // stud // student. ini ");

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 have the same meaning as writeprivateprofilestring.

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 ");

3. Write multiple values cyclically. If you have a program, you need to save several recently used file names. The specific program is as follows:

1. Write:

Cstring strtemp, strtempa;
Int I;
Int ncount = 6;
File: // there are 6 file names to save
For (I = 0; I {strtemp. Format ("% d", I );
Strtempa = file name;
File: // the file name can be obtained from an array or a list box.
: Writeprivateprofilestring ("usefilename", "FILENAME" + strtemp, strtempa,
"C: // usefile. ini ");
}
Strtemp. Format ("% d", ncount );
: Writeprivateprofilestring ("filecount", "Count", strtemp, "C: // usefile. ini ");
File: // write the total number of files for reading.

2. Read:

Ncount =: getprivateprofileint ("filecount", "Count", 0, "C: // usefile. ini ");
For (I = 0; I {strtemp. Format ("% d", I );
Strtemp = "FILENAME" + strtemp;
: Getprivateprofilestring ("currentini", strtemp, "Default. fil", strtempa. getbuffer (max_path), max_path, "C: // usefile. ini ");

File: // use the content in strtempa.

}

Four additional points:

1. The path of the INI file must be complete, and directories at all levels before the file name must exist. Otherwise, the write operation fails and the function returns false.

2. The file name path must be/, because in VC ++, // indicates /.

3. You can also put the INI file in the directory where the program is located. The lpfilename parameter is: ". // student. ini ".

How to read and write INI files using functions in VC

INI file (Initialization
File). This type of file usually stores the initialization information of a program. An INI file consists of several sections, each of which is composed of several key groups.
. Each key can be assigned a 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
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

determine the number of segments in an INI file, the simplest way is to find out all the section names and then count the number of section names. You can use the getprivateprofilesectionnames function to locate all the node names. The prototype is as follows:
DWORD getprivateprofilesectionnames (
lptstr lpszreturnbuffer, // points to a buffer zone, used to save the size of all the returned node names
DWORD nsize, // The lpszreturnbuffer parameter
lpctstr 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> tchar chsectionnames [2048] = {0 }; // character array composed of all node names
char * psectionname; // Save the first address of a node name string found
int I; // I points to a location of the array chsectionnames, starting from 0 and moving backward in sequence
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
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;
}

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.