Use Visual C ++ to operate on the INI file favorites

Source: Internet
Author: User

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. specific Application
As follows:

1. Write information to the. ini file.

1. the prototype of the winapi function used is:

Bool writeprivateprofilestring (
Lptstr lpappname,
Lpctstr
Lpkeyname,
Lptstr lpstring,
Lptstr lpfilename
);

 
Meanings of parameters:

Lptstr lpappname is a field name in the INI file.

Lpctstr
Lpkeyname is a key name under lpappname. Generally, it is a variable name.

Lptstr lpstring
It is the key value, that is, the value of the variable, but it 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 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]
Name = James

 
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,
Lpctstr
Lpkeyname,
Lptstr lpdefault,
Lptstr lpreturnedstring,
DWORD
Nsize,
Lptstr lpfilename
);

Each
Parameter meaning:

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 target cache.

Lpfilename: the complete INI file name.

2. Usage: Write the data in the previous step
The student information is read 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. to read an integer value, use another
Winapi functions:

Uint getprivateprofileint (
Lptstr lpappname,
Lpctstr
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 files in total
Save the part name
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.

}

Supplement
Four 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 ".

4. When you paste the source code from a Web page, it is best to paste it into notepad first, and then paste it
Paste it in VC. Otherwise, compilation errors may easily occur. At the beginning, I was very puzzled. Why isn't the code correct? Later, this method was found. Some codes also use full-width characters such as: <, \, etc.
Compilation error.

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.