Use Visual C ++ to operate 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]
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,
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 ".


 

Related Article

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.