VC reads and writes ini files

Source: Internet
Author: User

In the program we write, there is always some configuration information that needs to be saved to complete the program function. 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,
Lpctstr
Lpkeyname,
Lptstr lpstring,
Lptstr lpfilename
);

Meanings of parameters:
Lptstr lpappname is a field name in the INI file.
Lptstr lpkeyname
It is a key name under lpappname. Generally speaking, 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 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,
Lpctstr
Lpfilename
);
Meanings of parameters:
The first two parameters and
Writeprivateprofilestring has the same meaning.

Lpdefault:
If the INI file does not contain the field name or key name specified by the first two parameters, the 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:
Is 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 ");

Strstudname after execution
The 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
);

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 the directories at all levels before the file name must exist. Otherwise, the writing fails, and the function returns false.
Value.

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 pasting source code from a Web page, it is best to paste the source code to notepad first, and then paste it to VC. Otherwise, compilation errors may easily occur, and I will not be able to understand it at the beginning. How can this problem be solved if the code is good? Later, I found this method. Some codes also use full-width characters such as: <, \, which may cause compilation errors.

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.