在Delphi中打开/操作/保存TextFile

时间:2014-08-12 19:16:58

标签: delphi text-files

我是Delphi Noob,我搜索并逐步完成了我的代码而不理解为什么这会输出没有内容的文本文件。它应该在对话框中打开文本文件,它可以分配所选文件origFile,然后我想将该文件分配给另一个名为myFile的文件,这样我就可以在不改变实际原始文件的情况下进行操作,然后我想将myFile保存为(sOrigFileName + 'B.BACKWARDS.TXT'),但它跳过while not Eof(origFile) do并转到CloseFile。我在下面发布我的代码。

要求是使用TextFile vs TStringList,否则我会用TStringList完成此操作。另一个问题是它说明找不到myFile,我认为assignFile(myFile, sOrigFileName);正在创建myFile ....

procedure TForm1.btnOpenFileCClick(Sender: TObject);
var
    origFile, myFile : TextFile;
    OpenDialog1 : TOpenDialog;
    sOrigFileName, sNewFileName, sLine, sNewLine : String;

begin
    //Selects txt File of the users choosing
    OpenDialog1 := TOpenDialog.Create(self);
    OpenDialog1.Filter := '*.txt';

    if OpenDialog1.Execute then
    begin
      //AssignFile(origFile, OpenDialog1.FileName);
      sOrigFileName := OpenDialog1.FileName;
      AssignFile(origFile, sOrigFileName);
    end;

    //BACKWARDS
    sNewFileName := sOrigFileName + '.B.BACKWARDS.TXT';
    DeleteFile(sNewFileName);  //Delete old File if exists
    AssignFile(myFile, sNewFileName);

    Reset(origFile);

    while not Eof(origFile) do
    begin
       Rewrite(myFile);
       Readln(origFile, sLine); //takes line into a string Var
       sNewLine := AnsiReverseString(sLine); //reverses and places to  new Var
       WriteLn(myFile,sNewLine);
    end;

    //Clean up
    CloseFile(OrigFile);
    CloseFile(myFile);

2 个答案:

答案 0 :(得分:3)

请改为尝试:

procedure TForm1.btnOpenFileCClick(Sender: TObject);
var
  origFile, myFile : TextFile;
  OpenDialog1 : TOpenDialog;
  sOrigFileName, sNewFileName, sLine, sNewLine : String;
begin
  OpenDialog1 := TOpenDialog.Create(nil);
  try
    OpenDialog1.Filter := '*.txt';
    if not OpenDialog1.Execute then
      Exit;
    sOrigFileName := OpenDialog1.FileName;
  finally
    OpenDialog1.Free;
  end;

  sNewFileName := sOrigFileName + '.B.BACKWARDS.TXT';
  DeleteFile(sNewFileName);

  AssignFile(origFile, sOrigFileName);
  try
    Reset(origFile);

    AssignFile(myFile, sNewFileName);
    try
      Rewrite(myFile);

      while not Eof(origFile) do
      begin
        Readln(origFile, sLine);
        sNewLine := AnsiReverseString(sLine);
        WriteLn(myFile, sNewLine);
      end;
    finally
      CloseFile(myFile);
    end;
  finally
    CloseFile(OrigFile);
  end;
end;

我不知道为什么你需要使用TextFile,这是一种非常古老的学校方法而且根本不是现代的。如果您有权访问它们,则应使用TStreamReader/Writer

procedure TForm1.btnOpenFileCClick(Sender: TObject);
var
  origFile: TStreamReader;
  myFile : TStreamWriter;
  OpenDialog1 : TOpenDialog;
  sOrigFileName, sNewFileName, sLine, sNewLine : String;
begin
  OpenDialog1 := TOpenDialog.Create(nil);
  try
    OpenDialog1.Filter := '*.txt';
    if not OpenDialog1.Execute then
      Exit;
    sOrigFileName := OpenDialog1.FileName;
  finally
    OpenDialog1.Free;
  end;

  sNewFileName := sOrigFileName + '.B.BACKWARDS.TXT';
  DeleteFile(sNewFileName);

  origFile := TStreamReader.Create(sOrigFileName);
  try
    myFile := TStreamWriter.Create(sNewFileName, False, origFile.CurrentEncoding);
    try
      while not origFile.EndOfStream do
      begin
        sLine := origFile.ReadLine;
        sNewLine := AnsiReverseString(sLine);
        myFile.WriteLine(sNewLine);
      end;
    finally
      myFile.Free;
    end;
  finally
    OrigFile.Free;
  end;
end;

否则,请改用TStringList

procedure TForm1.btnOpenFileCClick(Sender: TObject);
var
  myFile : TStringList;
  OpenDialog1 : TOpenDialog;
  sOrigFileName, sNewFileName, sLine, sNewLine : String;
  I: Integer;
begin
  OpenDialog1 := TOpenDialog.Create(nil);
  try
    OpenDialog1.Filter := '*.txt';
    if not OpenDialog1.Execute then
      Exit;
    sOrigFileName := OpenDialog1.FileName;
  finally
    OpenDialog1.Free;
  end;

  sNewFileName := sOrigFileName + '.B.BACKWARDS.TXT';
  DeleteFile(sNewFileName);

  myFile := TStringList.Create;
  try
    myFile.LoadFromFile(sOrigFileName);

    for I := 0 to myFile.Count-1 do
    begin
      sLine := myFile[I];
      sNewLine := AnsiReverseString(sLine);
      myFile[I] := sNewLine;
    end;

    myFile.SaveToFile(sNewFileName);
  finally
    myFile.Free;
  end;
end;

答案 1 :(得分:2)

在我看来,好像你需要Rewrite(myFile)Reset(origFile)之后直接在循环之外。