INI file programming

xiaoxiao2021-04-10  354

Among the procedures we wrote, there are always some configuration information to be saved so that the functionality of the program is to write this information to the INI file. The program is initialized. The specific application is as follows:

1. Write the information in the .ini file.

1. The WinAPI function used in:

Bool writeprivateprofilestring

LPCTSTR LPAPPNAME,

LPCTSTR LPKEYNAME,

LPCTSTR LPSTRING,

LPCTSTR LPFILENAME

);

The meaning of each parameter:

LPCTSTR LPAPPNAME is a field name in the INI file.

LPCTSTR LPKEYNAME is a key name under LPAppname, which is a variable name.

LPCTSTR LPSTRING is a key value, that is, the value of the variable, but must be a LPCTSTR or CSTRING type.

LPCTSTR LPFILENAME is a complete INI file name.

2. Specific use: Set up an existing student, you need to write his name and age into the C: /Stud/student.ini file.

CString Strname, Strtemp;

Int nage;

Strname = "Zhang San";

NAGE = 12;

:: WritePrivateProfileString ("StudentInfo", "Name", Strname, "C: //stud/student.ini");

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

[Studentinfo]

Name = Zhang 3

3. To save the age of the students, simply try the value of the integer:

Strtemp.Format ("% D", NAGE);

:: WritePrivateProfileString ("StudentInfo", "AGE", Strtemp, "C: //stud/student.ini");

II. Seize the information from the INI file to the variables in the program.

1. The WinAPI function used in:

DWORD getPrivateProfileString

LPCTSTR LPAPPNAME,

LPCTSTR LPKEYNAME,

LPCTSTR LPDEFAULT,

LPTSTR LPRETURNEDSTRING,

DWORD NSIZE,

LPCTSTR LPFILENAME

);

The meaning of each parameter:

The first two parameters are the same as the meaning of WritePrivateProfileString.

LPDEFAULT: If there is no previous two parameter specified in the INI file, the value is assigned to the variable.

LPRETURNEDSTRING: The CString object that receives the value in the INI file, that is, the destination buffer.

Nsize: The size of the destination buffer.

LPFileName: is a complete INI file name.

2. Specific use: Now use the information of the student 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 stratudname is: "Zhang San", if the first two parameters are incorrect, its value is: "Default Name".

3. Read the integer value to use another WinAPI function:

Uint getprivateprofileint

LPCTSTR LPAPPNAME,

LPCTSTR LPKEYNAME,

Int ndefault,

LPCTSTR LPFILENAME

);

The parameters here are the same. The method is as follows:

NStudage = GetPrivateProfileint ("StudentInfo", "Age", 10, "C: //stud/student.ini"); III. Recycling multiple values, set there is a program, and several files to be used recently The name is saved, the specific procedures are as follows:

1. Write:

CString Strtemp, Strtempa;

INT I;

INT ncount = 6;

FILE: / / A total of 6 file names need to be saved

For (i = 0; i {straTemp.format ("% d", i);

STRTEMPA = file name;

File: // The file name can be obtained from arrays, list boxes, etc.

:: WritePrivateProfileString ("Usefilename", "FileName" Strtemp, Strtempa, "C: ///usefile//usefile.ini");

}

Strtemp.Format ("% D", NCOUNT);

:: WritePrivateProfileString ("Filecount", "Count", Strtemp, "C: //Usefile//Usefile.ini");

File: // Writes the total number of files to read.

2. Read:

Ncount = :: GetPrivateProfileint ("Filecount", "Count", 0, "C: //Usefile//usefile.ini");

For (i = 0; i {straTemp.format ("% d", i);

Strtemp = "filename" strTemp;

:: GetPrivateProfileString ("currentini", strtemp, "default.fil", strtempa.getBuffer (max_path), max_path, "c: ///usefile//usefile.ini");

File: // Use the contents of Strtempa.

}

Add four points:

The path to the 1.ini file must be complete, and the file names must exist in front of the file name, otherwise the write is unsuccessful, and the function returns the false value.

2. The file name must be //, because in VC , // represents one /.

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

4. When paste the source code from the web page, it is best to paste into the notepad, then paste it into the VC, otherwise it is easy to cause compilation errors, I am very unexpected when I started, how can I not find it? Later I found it. This method. There are also some code to use all the characters such as: <, \, etc.

Causes compilation errors.

INI file programming

Ini files have important roles in system configuration and application parameters, which is important, so visualized programming, such as VB, VC, VFP, Delphi, etc., provide methods of reading and writing Ini files, where Delphi is operating ini The file is the most concise, because Delphi3 provides a TiniFile class that allows us to handle the INI file very flexible.

First, it is necessary to understand the structure of the INI file:

Notes

[Small name]

Keyword = value

...

The INI file allows multiple sections, and each section is allowed to have multiple keywords, "=" later is the value of the keyword.

There are three types of values: strings, integer values, and Boolean values. There is no quota when the string is stored in the INI file.

Boolean true value is used in 1, and the Boolean value value is represented by 0.

Note Taking a semicolon ";" starting.

Second, definition

1. Add inIfiles in the Uses section of Interface;

2, add a line in the definition part of the VAR variable:

MyiniFile: tinifile;

Then, the variable MyiniFile can be created, opened, read, written, and other operations.

Third, open the INI file

Myinifile: = TiniFile.create ('program.ini');

The above row statement will be connected to the variable MyiniFile and the specific file Program.ini, and then you can read the keywords in the programm.ini file by variable myinifile. It is worth noting that if the file name in the parentheses does not specify the path, then this Program.ini file is stored in the Windows directory, and the method stored in the application of the Program.ini file in the current directory is: specify the complete Path and file name. The following two statements can complete this feature:

FileName: = ExtractFilePath (paramstr (0)) 'Program.ini';

Myinifile: = TiniFile.create (filename);

Fourth, read the value of the keyword

For three data types supported by INI files, integer values, Boolean data types, TiniFiles classes provide three different object methods to read the value of keywords in the INI file.

Suppose the variables Vs, VI, VB have been defined, respectively, Integer, and Boolean types.

VS: = MyiniFile.Readstring ('small name ",' keyword ', default);

VI: = MyiniFile.Readinteger ('small name ",' keyword ', default);

VB: = MyiniFile.ReadBool ('Name ",' Keyword ', default);

The default value is the default value returned when the INI file does not exist.

V. Write ini file

Similarly, the TiniFile class also provides three different object methods to write strings, integer and Boolean keywords to the INI file.

MyiniFile.WritString ('small name ",' keyword ', variable or string value);

MyiniFile.Writeinteger ('small name ",' keyword ', variable or integer value);

MyiniFile.WriteBool ('small name ",' keyword ', variable or true or false;

When this INI file does not exist, the above statement will automatically create the INI file.

Six, delete keywords

In addition to adding a keyword, the TiniFile class also provides an object method for deleting keywords:

MyiniFile.deletekey ('small name ",' keyword ');

Seven, section operation

Increases a method of using a write method to complete, delete a section available below:

MyiniFile.RaseSection ('small name');

In addition, the TiniFile class also provides three object methods to operate on the section:

MyiniFile.Readsection ('small name ", tstrings variable); you can read all keyword names in the specified section to a string list variable;

MyiniFile.Readsections (tstrings variable); you can read all the subordenses in the INI file to a string list variable.

MyiniFile.ReadsectionValues ​​('small name ", TStrings variable); all rows (including keywords, =, values) that specify in the INI file can be read to a string list variable. Eight, release

In the appropriate location, use the following statement to release MyiniFile:

MyiniFile.Distory;

Nine, one example

Below with a simple example (Figure), a method of establishing, reading, and storage INI files is demonstrated. The Myini.ini file contains three keywords with the "Program Parameters" section, and the user name (string), whether formal user (Boolean) and the run time (integer). The program is established in the form to read the data and write myini.ini files when the form is released.

Advanced program list

Unit unit1;

Interface

Uses

Windows, Messages, Sysutils, Classes, Graphics, Controls, Forms, Dialogs, INIFILES, STDCTRLS, EXTCTRLS

Type

TFORM1 = Class (TFORM)

EDIT1: TEDIT;

Checkbox1: Tcheckbox;

EDIT2: TEDIT;

Label1: TLABEL;

Label2: TLABEL;

Timer1: TTIMER;

Label3: TLABEL;

Procedure formcreate (Sender: TOBJECT);

Procedure FormDestroy (Sender: TOBJECT);

Procedure Timer1Timer (Sender: TOBJECT);

Private

{Private Declarations}

public

{Public declarations}

END;

VAR

FORM1: TFORM1;

IMPLEMENTATION

VAR

MyiniFile: tinifile;

{$ R * .dfm}

Procedure TFORM1.FormCreate (Sender: TOBJECT);

Var filename: String;

Begin

Filename: = ExtractFilePath (paramstr (0)) 'Myini.ini';

Myinifile: = TiniFile.create (filename);

Edit1.text: = myinifile.readstring ('program parameter', 'user name ",' default user name ');

Edit2.Text: = INTTOSTR (MyiniFile.Readinteger ('program parameter', 'has run time', 0));

Checkbox1.checked: = myinifile.readbool ('program parameter', 'is formal user', false);

END;

Procedure TFORM1.FORMDESTROY (Sender: TOBJECT);

Begin

MyiniFile.writestring ('program parameter', 'user name "; edit1.text);

MyiniFile.WriteInteger ('program parameter', 'has run time', strtoint (edit2.text));

MyiniFile.writeBool ('program parameter', 'is formal user', checkbox1.checked;

MyiniFile.Destroy;

END;

Procedure TFORM1.TIMER1TIMER (Sender: TOBJECT);

Begin

Edit2.Text: = INTTOSTR (strtOINT (Edit2.Text) 1);

END;

End.

转载请注明原文地址:https://www.9cbs.com/read-133317.html

New Post(0)