以下程序的目的是:单击按钮Button1,如果在F盘根目录下不存在文件log.txt,则创建该文件,并写一行字符串Hello,如已存在,则直接将该字符串写入文件中。
现在执行时,如果文件已经存在,则运行正常;如果不存在,则
运行到(**)处发生异常:I/O error 32
请哪位高手帮帮忙。不胜感激!
object Form1: TForm1
  Left = 192
  Top = 112
  Width = 696
  Height = 480
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -13
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 120
  TextHeight = 16
  object Button1: TButton
    Left = 240
    Top = 192
    Width = 75
    Height = 25
    Caption = 'Button1'
    TabOrder = 0
    OnClick = Button1Click
  end
end
unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;var
  Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var
  FileName:TextFile;
begin
  if not FileExists('f:\log.txt') then
    if FileCreate('f:\log.txt')=-1 then
    begin
      ShowMessage('Cannot create the log file!');
    end;
  AssignFile(FileName,'f:\log.txt');
  Append(FileName);                        (**)
  Writeln(FileName,'Hello');
  Flush(FileName);
  CloseFile(FileName);
end;end.

解决方案 »

  1.   

    用这个吧!
    var
      FileHandle: Integer;FileHandle := FileCreate(SaveDialog1.FileName);
    FileWrite(FileHandle,'hello', Length('hello'));
    FileClose(FileHandle);
      

  2.   

    文件不存在时要创建 Rewrite(FileName);procedure TForm1.Button1Click(Sender: TObject);
    var
      FileName:TextFile;
    begin
      if not FileExists('f:\log.txt') then
      begin
        AssignFile(FileName,'f:\log.txt');
        Rewrite(FileName);
        if not FileExists('f:\log.txt') then
        begin
          ShowMessage('Cannot create the log file!');
        end;
        CloseFile(FileName);
      end
      else
      begin
        AssignFile(FileName,'f:\log.txt');
        Append(FileName);
        Writeln(FileName,'Hello');
        Flush(FileName);
        CloseFile(FileName);
      end;
    end;