Delphi 在firemonkey中打开并读取文件
这个代码有什么问题?我不理解,如果我删除“尝试”,我的应用程序不会打开,如果不删除,总是显示“需要登录” 错误:[EConvertError]“09/11/2019”不是有效日期 要创建文件,我需要执行以下操作:Delphi 在firemonkey中打开并读取文件,delphi,firemonkey,Delphi,Firemonkey,这个代码有什么问题?我不理解,如果我删除“尝试”,我的应用程序不会打开,如果不删除,总是显示“需要登录” 错误:[EConvertError]“09/11/2019”不是有效日期 要创建文件,我需要执行以下操作: procedure TF_login.btn_entrarClick(Sender: TObject); var data : tdatetime; Resposta, data_s: string; begin PathFile := System.IOUtils.TP
procedure TF_login.btn_entrarClick(Sender: TObject);
var
data : tdatetime;
Resposta, data_s: string;
begin
PathFile := System.IOUtils.TPath.GetDocumentsPath;
NameFile := 'Limit.txt';
data := Now; //data actual
data := IncMonth(data, 2);
data_s := FormatDateTime('dd/mm/yyyy', data);
TFile.WriteAllText(TPath.Combine(PathFile, NameFile), data_s );
F_inicio.Show;
end;
该文件存在,因为第一个(和第二个)ShowMessage(注释内容)向我显示“09/11/19”,但第三个和第四个没有显示给我
OBS:delphi10.3(RIO),Plataform:Android在代码中有几点需要更改:
procedure TF_login.FormActivate(Sender: TObject);
var
TextFile: TStringList;
VarArquivo: string;
text: string;
dataI, dataF : string;
begin
// If an exception (unlikely, but on principle) happens in your VarArquivo
// assignment, then the original version will leak the allocated TStringList.
// Always place the TRY right after allocation of a memory block. That way
// you ensure that the FINALLY block will always release the allocated
// memory. Also, always include a FINALLY block to release the memory. Don't
// count on your code to reach the FreeAndNIL code (it doesn't in this
// instance, as you can see) to make sure that you actually release the
// memory.
VarArquivo := System.IOUtils.TPath.GetDocumentsPath + PathDelim + 'Limit.txt';
TextFile := TStringList.Create;
try // - except
try // - finally
TextFile.LoadFromFile(VarArquivo);
text := TextFile.Text;
// ShowMessage(TextFile.Text); // there is the text
// ShowMessage(text); // there is the text
dataI := FormatDateTime('yyyy/mm/dd', Date);
dataF := FormatDateTime('yyyy/mm/dd', StrToDate(text));
ShowMessage(dataF +' data f');
ShowMessage(dataI +' data I');
if ( dataF < dataI ) then
begin
ShowMessage('data F low');
end
else
begin
ShowMessage('data F high');
F_inicio.Show;
end;
finally
FreeAndNil(TextFile);
end
except
// NEVER just "eat" an exception. Especially not while developing the
// application.
// Always either log the exception or show it to the user.
on E:Exception do ShowMessage('Exception '+E.ClassName+': '+E.Message+#13#10+
'need login');
end;
end;
程序tfu\u login.FormActivate(发送方:TObject);
变量
文本文件:TStringList;
瓦拉奎沃:弦;
文本:字符串;
dataI,dataF:string;
开始
//如果在您的VarArquivo中发生异常(不太可能,但原则上)
//赋值,则原始版本将泄漏已分配的TStringList。
//始终在分配内存块后立即放置TRY。那样
//确保FINALLY块始终释放分配的
//记忆。另外,始终包括一个FINALLY块以释放内存。不要
//依靠你的代码来获得FreeAndNIL代码(在本例中没有)
//实例,如您所见)以确保实际释放
//记忆。
VarArquivo:=System.IOUtils.TPath.GetDocumentsPath+PathDelim+'Limit.txt';
TextFile:=TStringList.Create;
尝试//-除了
最后试试
TextFile.LoadFromFile(VarArquivo);
text:=TextFile.text;
//ShowMessage(TextFile.Text);//这是文本
//ShowMessage(文本);//这是文本
dataI:=格式日期时间('yyyy/mm/dd',日期);
dataF:=FormatDateTime('yyyy/mm/dd',StrToDate(text));
ShowMessage(dataF+‘dataF’);
ShowMessage(数据I+‘数据I’);
如果(dataF
现在-如果您这样做,将显示什么异常和错误消息。这是正确诊断错误所必需的。当你看到问题的症结所在时,也许你甚至可以自己找出答案。评论不适用于长时间的讨论;此对话已结束。请使用调试器查明代码失败的确切位置。目前,我们可以看到引发了异常,但您将其与它提供的所有信息一起吞并。谢谢,但已经告诉我了,我更改了代码,错误是:`“[EConvertError]“2019年11月9日”不是有效日期“`
Result:=CompareDate(strotate(dataI),strotate(dataF))代码>生成comparison@user3602803:这就是你想要的。字符串“09/11/2019”不是运行该字符串的计算机的默认日期格式中指定的有效日期。这个日期是从哪里来的?是DD/MM/YYYY还是MM/DD/YYYY格式?它总是相同的格式吗?如果是这样,您可以向日期转换函数提供一个额外的参数,该参数告诉字符串中使用的格式,而不是依赖您正在运行的机器的日期格式与最初生成字符串的机器的日期格式相同……最后,我发现了错误所在,捕获文本文件时的“text”变量,插入了一个换行符,这是一个不可见的东西,因为除了日期之外没有其他东西。我在我的代码中插入了text:=copy(text,1,10),这是有效的:)@user3602803我可能会使用text:=TextFile.text.Trim,而不是假设行的长度为10个字符。
procedure TF_login.FormActivate(Sender: TObject);
var
TextFile: TStringList;
VarArquivo: string;
text: string;
dataI, dataF : string;
begin
// If an exception (unlikely, but on principle) happens in your VarArquivo
// assignment, then the original version will leak the allocated TStringList.
// Always place the TRY right after allocation of a memory block. That way
// you ensure that the FINALLY block will always release the allocated
// memory. Also, always include a FINALLY block to release the memory. Don't
// count on your code to reach the FreeAndNIL code (it doesn't in this
// instance, as you can see) to make sure that you actually release the
// memory.
VarArquivo := System.IOUtils.TPath.GetDocumentsPath + PathDelim + 'Limit.txt';
TextFile := TStringList.Create;
try // - except
try // - finally
TextFile.LoadFromFile(VarArquivo);
text := TextFile.Text;
// ShowMessage(TextFile.Text); // there is the text
// ShowMessage(text); // there is the text
dataI := FormatDateTime('yyyy/mm/dd', Date);
dataF := FormatDateTime('yyyy/mm/dd', StrToDate(text));
ShowMessage(dataF +' data f');
ShowMessage(dataI +' data I');
if ( dataF < dataI ) then
begin
ShowMessage('data F low');
end
else
begin
ShowMessage('data F high');
F_inicio.Show;
end;
finally
FreeAndNil(TextFile);
end
except
// NEVER just "eat" an exception. Especially not while developing the
// application.
// Always either log the exception or show it to the user.
on E:Exception do ShowMessage('Exception '+E.ClassName+': '+E.Message+#13#10+
'need login');
end;
end;