Using API functions in VC to operate INI files

Source: Internet
Author: User
Tags key string

The following functions are used to operate the INI file in VC:

Function Name Function Remarks
Getprivateprofileint Reads the integer corresponding to the key name in the specified block of the INI file.
Getprivateprofilesection Record all key names in the specified block of the INI file and their corresponding values.
Getprivateprofilesectionnames Read all the block names in an INI file.
Getprivateprofilestring Reads the string corresponding to the key name in the specified block of the INI file.
Getprivateprofilestruct Reads the data corresponding to the key name in the specified block of the INI file.
Getprofileint Reads the integer corresponding to the key name in the specified block in win. ini.
Getprofilesection Read all the key names and their values in the specified block in win. ini.
Getprofilestring

Read the corresponding value of the key name in the specified block in win. ini.

Writeprivateprofilesection Replace the values of all key names in the specified block in the INI file.
Writeprivateprofilestring Write the given key name and value to the corresponding block of the specified INI file.
Writeprivateprofilestruct Write the specified key name and data to the block of the specified INI file.
Writeprofilesection Replace the values of all key names of the specified block in win. ini.
Writeprofilestring Write the given key name and value to the corresponding block in win. ini.

 

INI files can be stored and shared with applications.ProgramA small amount of data, the format is generally:

[Block name 1]
Key name = Value
...
[Block name N]
Key name = Value

Generally, an INI file can have n blocks, and the block can have n key names and values. Each key name and value occupies one row.
Generally, the key name can be used. However, we recommend that you use meaningful characters and words. The value can be an integer or a string, and Other types must be converted.

Getprofileint-obtains an integer of the key from a section in the win. ini file. Its original form is:

Getprofileint (
Lptstr lpappname, // point to the string address containing the section name
Lptstr lpkeyname, // point to the string address containing the key name
Int ndefault // if the key value is not found, the default value is returned.
);

If the key value is not found, the returned value is the default value specified by ndefault. If the value in the key is negative, 0 is returned. If the key is a combination of numbers and strings, returns the value of the number. For example, if X = 1234 ABCD, 1234 is returned.

Getprofilestring-obtains a key string from a section in the win. ini file. Its original form is:

Getprofilestring (
Lptstr lpappname, // point to the string address containing the section name
Lptstr lpkeyname, // point to the string address containing the key name
Lptstr lpdefault, // if the key value is not found, the default string address is returned.
Lptstr lpreturnedstring, // return the buffer address of the string
DWORD nsize // buffer Length
);

The returned string is in the buffer, and the returned eax value is the length of the returned string (excluding the end 0)

Getprofilesection-read the content of the entire section from the win. ini file. Its original form is:

Getprofilesection (
Lptstr lpappname, // point to the string address containing the section name
Lptstr lpreturnedstring, // The buffer address of the returned data
DWORD nsize // The buffer length of the returned data
);

Writeprofilesection-Write the value of an entire section to the specified section in the win. ini file. Its prototype is:

Writeprofilesection (
Lptstr lpappname, // point to the string address containing the section name
Lptstr lpstring // the address of the data to be written
);

If the section is not specified by win. ini, the API creates a new section and writes data. If the Section already exists, delete all the key values in the original seciton and write the new section.

Writeprofilestring-write a key value to the specified section of the win. ini file. Its prototype is:

Writeprofilestring (
Lptstr lpappname, // point to the string address containing the section name
Lptstr lpkeyname, // point to the string address containing the key name
Lptstr lpstring // string address to be written
);

If no section is specified for win. ini, the API creates a section. If no key is specified, a new key is created and data is written. If yes, a string is used to replace the original value.
The above API is for win. INI operations, of course, for us, we use more to create our INI files in the directory where the program runs. If you need to operate on your INI files, another group of APIS is required. This group of APIS is similar to the above one. You only need to change the profile of the above group to privateprofile (private, the parameter also has an INI file name parameter. For example, getprivateprofileint, getprivateprofilesection, and writeprivateprofilestring are described as follows:

Getprivateprofileint-obtains an integer of the key from a section in the INI file. Its original form is:

Getprivateprofileint (
Lptstr lpappname, // point to the string address containing the section name
Lptstr lpkeyname, // point to the string address containing the key name
Int ndefault // if the key value is not found, the default value is returned.
Name of the lpfilename // INI File
);

The definitions of intermediate parameters and returned values are the same as those of getprofileint.

Getprivateprofilestring-obtains a key string from a section of the INI file. Its original form is:

Getprivateprofilestring (
Lptstr lpappname, // point to the string address containing the section name
Lptstr lpkeyname, // point to the string address containing the key name
Lptstr lpdefault, // if the key value is not found, the default string address is returned.
Lptstr lpreturnedstring, // return the buffer address of the string
DWORD nsize // buffer Length
Name of the lpfilename // INI File
);

Getprivateprofilesection-read the content of the entire section from the INI file. Its original form is:

Getprivateprofilesection (
Lptstr lpappname, // point to the string address containing the section name
Lptstr lpreturnedstring, // The buffer address of the returned data
DWORD nsize // The buffer length of the returned data
Name of the lpfilename // INI File
);

This API can read the content of the entire section. If you do not know the keys in the section, you can use this API to read the entire section and then process it.

Note:

For example, the INI file is:

[Student]

Name = Tian

Age = 20

Sex = man

ByGetprivateprofilesection() Second parameter of the FunctionLpreturnedstringThe returned string is as follows:

Name = Tian "0age = 20" 0sex = Man "0" 0 (each entry is separated by "0" and ends with two "0)

We want to wait until each key and its corresponding value. How can we split the above string? In VB, there is a split () function, which is easy to use, but not in VC.

Of course, you can also implement a split () function in VC, but here I provide a simple method:

Cstring strkey;

While (* Str! = '"0') // STR isLpreturnedstringReturned string

{

Strkey = STR;

MessageBox (strkey); // strkey is each key and its corresponding value

STR + = strkey. getlength () + 1;

}

Getprivateprofilesectionnames-obtain the section name from the INI file. Its original form is:

Getprivateprofilesectionnames (
Lptstr lpszreturnbuffer, // The buffer address of the returned data
DWORD nsize // The buffer length of the returned data
Name of the lpfilename // INI File
);

If ini contains two sections: [sec1] and [sec2], the returned sections are 'sec1', 0, 'sec2, you can use this API to obtain the name of a section in ini.

Writeprivateprofilesection-import the content of an entire section into the specified section of the INI file. Its original form is:

Writeprivateprofilesection (
Lptstr lpappname, // point to the string address containing the section name
Lptstr lpstring // the address of the data to be written
Name of the lpfilename // INI File
);

Writeprivateprofilestring-write a key value to the specified section of the INI file. Its prototype is:

Writeprivateprofilestring (
Lptstr lpappname, // point to the string address containing the section name
Lptstr lpkeyname, // point to the string address containing the key name
Lptstr lpstring // string address to be written
Name of the lpfilename // INI File
);

If no section is specified in ini, the API creates a section. If no key is specified, a new key is created and data is written. If yes, the original value is replaced by a string. When the specified ini does not exist, the API will automatically create a new file, so the advantage of using INI is that we do not have to involve file operations to save a small amount of data, it is unnecessary to search for the existence of a file.

Usage tips:

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 has no path, the API will go to the installation directory of Windows instead of the current directory, but it is too troublesome to get the current path every time you use the INI function. Here is a work und, you only need to add. "That's okay, for example, the user in the current directory. INI operation, the file name is '. "user. INI 'is obviously more convenient. In addition, to clear a key, you can point lpstring to an empty string and then use writeprivateprofilestring. When you want to clear all the content of a section, you do not need to clear the keys one by one. You can point the lpstring to an empty string and then use writeprivateprofilesection.

--------------------------------

Among the programs we write, there 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 it 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
). The meanings of parameters are as follows:

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
). The meanings of parameters are as follows:

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 strstudname value 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
); Here the parameter meaning is the same 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" "usefile. ini ");
}
Strtemp. Format ("% d", ncount );
: Writeprivateprofilestring ("filecount", "Count", strtemp,
"C:" "usefile" "usefile. ini ");
File: // write the total number of files for reading. 2. Read:

Ncount =: getprivateprofileint ("filecount", "Count", 0,
"C:" "usefile" "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" "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".

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.