INI file (xxx.ini), which is the system configuration file, used to save some configuration information of the system.
Using the INI configuration file
In BCB there is a Tinifile class, which provides the operation of the INI file, INI file a common configuration file format It also like the registry as the key and data items, the following is the contents of an INI configuration file, it has a key lastconfig, Under this key is a data item defaultdit, which is the value of the data item after the data item.
[Lastconfig]
Defaultdir=e:\xxx
The Tinifile class has several common methods to introduce to you:
__fastcalltinifile (const ansistring FileName): inifiles::tcustominifile (filename) {}
This method creates an INI object that automatically generates the file if the filename specified by filename does not exist, and filename can be used as the full path, specifying the location of the INI file, and if only the file name does not have a path, the function will be opened or created on the Windows system path. (Win9x is the Windows directory, NT and 2000 are WINNT directories)
BOOL __fastcall sectionexists (const ansistring section);
This method determines if there is a section key.
Virtual ansistring __fastcall ReadString (const ansistring section, const ansistring Ident, const ansistring Default);
This method reads a string from the INI file,
The section is the specified key,
Ident is the default value of the data item under the specified key, if the item does not exist.
virtual void __fastcall writestring (const ansistring section, const ansistring Ident, const ansistring Value);
This method is to write a string to the INI file,
Senction is the specified key,
Ident is the specified data item,
Value is the data to write.
Like Tregistry, the Tinifile class also provides read-write methods for reading integer, Boolean, binary, and other data types, so you can look at the online Help and use the same basic.
Here is an example of the INI file above to illustrate how to have an INI file to save the configuration information and how to read the data from the INI file.
The following example is how to read the configuration from an INI file, which is saved in the application's directory, and if not configured, the path to the accessor is the default value.
#include <inifiles.hpp>
void __fastcall Readconfig ()
{//Read configuration,
Tinifile * RegKey;
Ansistring ExePath = extractfilename (paramstr (0));//returns the working path of the specified file
RegKey = new Tinifile (ExePath + "Trimtxt.ini");
Defaultdir=regkey->readstring ("Lastconfig", "DefaultDir", ExePath);
Delete RegKey;
}
The following example shows how to write information to the INI file
void __fastcall Saveconfig ()
{
Tinifile * RegKey;
Ansistring ExePath = extractfilename (paramstr (0));
RegKey = new Tinifile (ExePath + "Trimtxt.ini");
Regkey->writestring ("Lastconfig", "DefaultDir", DefaultDir);
Delete RegKey;
}
It is convenient to use INI file to save configuration information, the biggest advantage is that the user can manually change it, it is usually used to save some of the data is not critical. Its disadvantage is that it is not suitable for storing complex data, and the file is easily destroyed.
Http://blog.hehehehehe.cn/a/8454.htm
Functions and explanations used by MP3:
(1) Change the extension of the specified file:
function Changefileext (const FileName, extension:string): string;
The first parameter is the name of the file you want to modify, with a path
The second parameter is a modified suffix name
The function returns the modified file name
Eg:changefileext (Mp3.exe, config.cfg) ===> return mp3.cfg
For example: S:=changefileext (' f:\123.txt ', '. ini ');
ShowMessage (s);//f:\123.ini