求教以下代码如何修改?
//顶部uses小节加入IniFiles,uses
IniFiles,
==================================================
var
inifile:TIniFile; //定义ini文件
Filename:string; //其文件名类型
Keyword1: string;
Keyword2: Integer;
Keyword3: Boolean;
Keyword4: TDateTime;================》看这里,对不?
Keyword5: TDateTime;================》看这里,对不?
Keyword6: Real;
Begin
Filename:=GetCurrentDir + '\配置.ini';
inifile:=TIniFile.Create(PChar(Filename));
inifile.WriteBool('小节名1','关键字4',2020-8-8); //写入ini=====》报错!
Keyword4:=inifile.ReadDate('小节名1','关键字4',Keyword4); //读取ini
ShowMessage(DateToStr(Keyword4));
//inifile.DeleteKey('小节名','keyword4'); //删除关键字
//inifile.EraseSection('小节名');
inifile.free; //释放或 写成 inifile.distory;
End;
=======================================================
//顶部uses小节加入IniFiles,
uses
IniFiles,
==================================================
var
inifile:TIniFile; //定义ini文件
Filename:string; //其文件名类型
Keyword1: string;
Keyword2: Integer;
Keyword3: Boolean;
Keyword4: TDateTime;
Keyword5: TDateTime;=================》看这行,对不?
Keyword6: Real;
Begin
Filename:=GetCurrentDir + '\配置.ini';
inifile:=TIniFile.Create(PChar(Filename));
inifile.WriteBool('小节名1','关键字5',18:58); //写入ini===============》这里报错
Keyword5 := inifile.ReadTime ('小节名1','关键字5',Keyword5); //读取ini
ShowMessage(TimeToStr(Keyword5));
//inifile.DeleteKey('小节名','keyword5'); //删除关键字
//inifile.EraseSection('小节名');
inifile.free; //释放或 写成 inifile.distory;
End;
本帖最后由 lies2014 于 2021-9-12 11:05 编辑
WriteBool的最后一个参数是布尔值,你这给的啥也不是,自然出错
https://docwiki.embarcadero.com/ ... omIniFile.WriteBool
猜想你是想要写入时间吧,应该用WriteString,最后一个参数将要写入的时间先转换成字符串
ini文件的写入支持Bool、String、Integer,分别对应不同的过程 lies2014 发表于 2021-9-12 10:57
WriteBool的最后一个参数是布尔值,你这给的啥也不是,自然出错
https://docwiki.embarcadero.com/ ... om ...
@lies2014
对啊,读写INI;还有另外三种date、time、float(real已搞定)
date、time的变量类型
和
对应的格式不太明白到底咋写? Delphi自带了源码和很多示例的,看这些是最快的学习手段,下面是fpc自带的例程,用法非常清楚:
program inifmt;
{$mode objfpc}{$H+}
uses
{$IFDEF UNIX}{$IFDEF UseCThreads}
cthreads,
{$ENDIF}{$ENDIF}
Classes, IniFiles, SysUtils
{ you can add units after this };
var
ini: TCustomIniFile;
x: Double;
t: TTime;
d: TDate;
dt: TDateTime;
iniName: String = 'test.ini';
L: TStringList;
i: Integer;
begin
x := 1.2345;
t := time();
d := date();
dt := now();
ini := TMemIniFile.Create(iniName);
ini.FormatSettings.DecimalSeparator := '|';
ini.FormatSettingsActive := true;
ini.WriteFloat('Data', 'float', 1.2345);
ini.WriteTime('Data', 'time', t);
ini.WriteDate('Data', 'date', d);
ini.WriteDateTime('Data', 'datetime', dt);
ini.Free;
WriteLn('-----------------------------------------------------------');
WriteLn('Ini file (direct file content)');
WriteLn('-----------------------------------------------------------');
L := TStringList.Create;
L.LoadfromFile(ininame);
for i:=0 to L.Count-1 do
WriteLn(L);
L.Free;
WriteLn;
ini := TMemIniFile.Create(iniName);
ini.FormatSettings.DecimalSeparator := '|';
ini.FormatSettingsActive := true;
x := ini.ReadFloat('Data', 'float', 0);
t := ini.ReadTime('Data', 'time', 0);
d := ini.ReadDate('Data', 'date', 0);
dt := ini.ReadDateTime('Data', 'datetime', 0);
ini.Free;
WriteLn('------------------------------------------------------------------');
WriteLn('Read input data from ini file (output using DefaultFormatSettings)');
WriteLn('------------------------------------------------------------------');
WriteLn('float = ', FloatToStr(x));
WriteLn('time = ', TimeToStr(t));
WriteLn('date = ', DateToStr(d));
WriteLn('date/time = ', DateTimeToStr(dt));
WriteLn;
{$IFDEF MSWINDOWS}
WriteLn('Press to quit...');
ReadLn;
{$ENDIF}
end.
页:
[1]