Read/write INI configuration file

Source: Internet
Author: User
Tags function prototype ini open ini file


Please respect the original: Original address: Click to open the link





:: WritePrivateProfileString (_t ("Options"), _t ("Name"), M_strname, Papp->m_szini);

INI file programming, WINAPI function Writeprivateprofilestring,getprivateprofilestring Collection
In the program we write, there are always some configuration information to be saved, in order to complete the function of the program, the simplest way is to write this information into the INI file, the program is initialized to read again. The application is as follows:
First. Writes information to the. ini file.
1. The WINAPI function prototype used is:
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 lpappname under the name of a key, popularly speaking is the variable name.
LPCTSTR lpstring is a key value, which is the value of a variable, but must be of type LPCTSTR or CString.

LPCTSTR lpFileName is the full INI file name.

2. How to use:

 an existing student is required 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 contents of this C:\stud\student.ini file are as follows:
[Studentinfo]

Name= Zhang San
3. To save the student's age, simply change the value of the integer to the character type:
Strtemp.format ("%d", nAge);
:: WritePrivateProfileString ("Studentinfo", "Age", strtemp, "C:\\stud\\student.ini");

Second. Read the information from the INI file into the variables in the program.
1. The WINAPI function prototype used is:
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 in WritePrivateProfileString.

Lpdefault: If the INI file does not have the field name or key name specified by the first two parameters, assign this value to the variable.
Lpreturnedstring: The CString object that receives the value in the INI file, which is the destination cache.
NSize: The size of the destination buffer.
lpFileName: Is the full INI file name.
2. How to use: 

The student's information written in the previous step is now read into the program.
CString Strstudname;
int nstudage;
GetPrivateProfileString ("Studentinfo", "name", "Default name", Strstudname.getbuffer (MAX_PATH), MAX_PATH, "c:\\stud\\ Student.ini ");
The value of Strstudname after execution is: "Zhang San", if the first two parameters are incorrect, the value is "default name".

3. Read-in integer value to use another WINAPI function:
UINT Getprivateprofileint (
LPCTSTR Lpappname,
LPCTSTR Lpkeyname,
INT Ndefault,
LPCTSTR lpFileName
);
The parameters here have the same meaning as above. Use the following method:

Nstudage=getprivateprofileint ("Studentinfo", "age", Ten, "C:\\stud\\student.ini");

Third. Loop write multiple values, set up an existing program, to save the most recently used file names, the specific program is as follows:
1. Write:
CString Strtemp,strtempa;
int i;
int ncount=6;
file://a total of 6 file names to save

For (I=0;i {strtemp.format ("%d", I);
strtempa= file name;
file://file names can be obtained from arrays, list boxes, and so on.
:: 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 in order to be read out.

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://uses the content in Strtempa.
}

Add Four points:

The path to the 1.INI file must be complete, the directory must exist before the file name, or the write is unsuccessful, and the function returns a value of FALSE.

2. The path of the file name must be \ \, because in VC + +, \ \ only represents a \.

3. The INI file can also be placed in the directory where the program is located, at this time the lpFileName parameter is: ". \\student.ini".

4. From the Web page to paste the source code, it is best to paste into Notepad, and then to the VC paste, otherwise prone to compile errors, at the beginning I was also very puzzled, good code how is wrong? later found this method. There is also some code that uses the full-width word such as: <,\, also
Cause a compilation error.

INI file programming

INI file in the system configuration and application parameters to save and set up, has a very important role, so the visual programming of a family, such as VB, VC, VFP, Delphi, etc. have provided a way to read and write INI file, in which Delphi operation INI file, the most concise, This is because DELPHI3 provides a tinifile class that allows us to handle INI files with great flexibility.

First, it is necessary to understand the structure of the INI file:
; note
[section name]
Keyword = value
...
INI file allows multiple subsections, each of which allows for multiple keywords, followed by "=" with the value of the keyword.
There are three types of values: String, Integer value, and Boolean value. Where strings are stored in the INI file without quotation marks,
Boolean true value is represented by 1, and Boolean false values are expressed in 0.
Comment with a semicolon ";" Beginning.
Second, the definition
1, the uses festival in interface increased inifiles;
2. Add a line to the var variable definition section:
Myinifile:tinifile;
The variable myinifile can then be created, opened, read, written, and so on.

Third, open INI file


Myinifile:=tinifile.create (' Program.ini ');


This line of statements will establish a link between the variable myinifile and the specific file Program.ini, and then you can read and write the values of the keywords in the Program.ini file through the variable myinifile. It is worth noting that if the file name in parentheses does not indicate a path, then the Program.ini file is stored in the Windows directory, and the Program.ini file is stored in the application's current directory by specifying the full path and file name. The following two statements can complete this function:

Filename:=extractfilepath (paramstr (0)) + ' Program.ini ';

Myinifile:=tinifile.create (filename);

Fourth. reading the value of a keyword
The Tinifiles class provides three different object methods to read the values of the keywords in the INI file for the string, integer numeric, and Boolean three data types supported by the INI file.
Assume that the variables defined in VS, vi, and VB are string, Integer, and Boolean types, respectively.

Vs:=myinifile. Readstring (' Bar name ', ' keyword ', default value);
Vi:=myinifile. Readinteger (' Bar name ', ' keyword ', default value);
Vb:=myinifile. Readbool (' Bar name ', ' keyword ', default value);

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

Fifth. Write INI file
Similarly, the Tinifile class provides three different object methods, writing strings, integers, and Boolean types to the INI file.

Myinifile.writestring (' Bar name ', ' keyword ', variable or string value);
Myinifile.writeinteger (' Bar name ', ' keyword ', variable or integer value);
Myinifile.writebool (' Bar name ', ' keyword ', variable or TRUE or false);
When this INI file does not exist, the above statement will also automatically create the INI file.
Sixth. deletion of Keywords
In addition to using the Write method to add a keyword, the Tinifile class also provides an object method for deleting the keyword:
Myinifile. DeleteKey (' Bar name ', ' keywords ');

Seventh, subsection operation
Adding a section can be done by writing the method, deleting a section using the following object method:
Myinifile. Erasesection (' subsection name ');
In addition, the Tinifile class provides three object methods to manipulate the subsections:
Myinifile.readsection (' Bar name ', tstrings variable); All key names in the specified section can be read into a string list variable;
Myinifile.readsections (tstrings variable); All the section names in the INI file can be read into a string list variable.
Myinifile.readsectionvalues (' Bar name ', tstrings variable), all rows of the specified section in the INI file (including keywords, =, values) can be read into a string list variable.

Eighth. Release
In the appropriate location, release myinifile with the following statement:
Myinifile.distory;
Ninth. instances
The following is a simple example (pictured) that demonstrates the method of building, reading, and storing INI files. The Myini.ini file contains the "program parameters" subsection, and the user name (string), whether the official user (Boolean), and elapsed time (integer value) three keywords. The program reads the data in the form setup and writes the Myini.ini file when the form is released.
List of attached source programs
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 parameters ', ' User name ', ' Default user name ');
edit2.text:= IntToStr (Myinifile.readinteger (' program parameters ', ' Elapsed Time ', 0));
CheckBox1. checked:= myinifile.readbool (' program parameters ', ' Whether official user ', False);
End

Procedure Tform1.formdestroy (Sender:tobject);
Begin
Myinifile.writestring (' program parameters ', ' User name ', Edit1. Text);

Myinifile.writeinteger (' program parameters ', ' Elapsed Time ', Strtoint (Edit2.text));
Myinifile.writebool (' program parameters ', ' whether the official user ', CheckBox1. Checked);
Myinifile. Destroy;
End

Procedure Tform1.timer1timer (Sender:tobject);
Begin
Edit2. Text:=inttostr (Strtoint (Edit2.text) +1);
End

End.
The program is debugged and passed under Pwin95 and Delphi3.

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.