【问题标题】:Open and read a file in firemonkey在 firemonkey 中打开并读取文件
【发布时间】:2019-09-06 14:37:25
【问题描述】:

这段代码有什么问题?我不明白,如果我删除“尝试”我的应用程序不会打开,如果不删除总是出现“需要登录”...

procedure TF_login.FormActivate(Sender: TObject);
 var
      Result: Integer;
      TextFile: TStringList;
      VarArquivo: string;
      text: string;
      dataI, dataF : string;
begin
  TextFile := TStringList.Create;
  VarArquivo := System.IOUtils.TPath.GetDocumentsPath + PathDelim + 'Limit.txt';
  try
    TextFile.LoadFromFile(VarArquivo);

    text := TextFile.Text;
//    ShowMessage(TextFile.Text); // there is the text
//    ShowMessage(text); // there is the text

    dataI := FormatDateTime('dd/mm/yyyy', Now);
    dataF := FormatDateTime('dd/mm/yyyy', StrToDate(text));

    Result := CompareDate(StrToDate(dataI), StrToDate(dataF));
    ShowMessage(dataF +' data f');
    ShowMessage(dataI +' data I');
    if ( Result = LessThanValue ) then
    begin
      ShowMessage('data F low');
    end
    else
    begin
      ShowMessage('data F high');
      F_inicio.Show;
    end;

    FreeAndNil(TextFile);
  except on E:
    Exception do ShowMessage('An error happened!' + sLineBreak + '[' + 
                               E.ClassName + '] ' + E.Message);
  end;
end;

错误:[EConvertError] '09/11/2019' 不是有效日期

要创建文件,我会这样做:

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:Delphi 10.3 (RIO),平台:Android

【问题讨论】:

  • 评论不用于扩展讨论;这个对话是moved to chat
  • 使用调试器准确找出代码失败的地方。目前我们可以看到引发了一个异常,但是您将其连同它提供的所有信息一起吞下。

标签: delphi firemonkey


【解决方案1】:

您应该在代码中更改几件事:

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;

现在 - 如果您这样做,会显示什么异常和错误消息。这是正确诊断错误所必需的。当您看到到底出了什么问题时,也许您甚至可以自己弄清楚...

【讨论】:

  • 谢谢,但我已经在谈论这个了,我更改了代码,错误是:` "[EConvertError] '09/11/2019' is not a valid date "`
  • Result := CompareDate(StrToDate(dataI), StrToDate(dataF)); 进行比较的代码行
  • @user3602803:你有它(如预期的那样)。字符串 '09/11/2019' 不是在运行它的机器的默认日期格式中指定的有效日期。这个日期是从哪里来的?是 DD/MM/YYYY 还是 MM/DD/YYYY 格式?它总是相同的格式吗?如果是这样,您可以为日期转换函数提供一个额外的参数,该参数告诉字符串中使用的格式,而不是依赖于您运行的机器具有与首先生成字符串的机器相同的日期格式。 .
  • 最后,我发现了错误是什么,捕获文本文件时的“text”变量,插入了换行符,因为没有超过日期,所以不明显。我在我的代码中插入了 text := copy(text, 1, 10) ,这有效:)
  • @user3602803 我可能会使用 text := TextFile.Text.Trim 而不是假设该行有 10 个字符长。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-12-09
  • 1970-01-01
  • 1970-01-01
  • 2011-05-11
  • 2019-07-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多