虽然标示符已经写得很清楚了,但是我还是对其中的确切含义不理解,
我打开文件用的是fmOpenReadAndWrite,但是我想问的是,写文件的时候,为什么还需要read呢?

解决方案 »

  1.   

    FileOpen    开档.
    -----------------------------------------------------------------------------
    Unit   SysUtils
    函数原型 function FileOpen(const FileName: string; Mode: 
         Integer):Integer;
    ****   开档失败传回-1.
    说明   以下有关档案读取都属低阶,如Dos Int 21h中有关档案的部
        分.
        fmOpenRead    = $0000;
        fmOpenWrite    = $0001;
        fmOpenReadWrite   = $0002;
        fmShareCompat   = $0000;
        fmShareExclusive   = $0010;
        fmShareDenyWrite   = $0020;
        fmShareDenyRead   = $0030;
        fmShareDenyNone   = $0040;    fmOpenRead    Open for read access only.
        FmOpenWrite    Open for write access only.
        FmOpenReadWrite   Open for read and write access.
        fmShareCompat   Compatible with the way FCBs are 
             opened.
        fmShareExclusive   Read and write access is denied.
        fmShareDenyWrite   Write access is denied.
        fmShareDenyRead   Read access is denied.
        fmShareDenyNone   Allows full access for others.
    范例
    procedure OpenForShare(const FileName: String);
    var
       FileHandle : Integer;
    begin
       FileHandle := FileOpen(FileName, fmOpenWrite or fmShareDenyNone);
       if FileHandle > 0 then
         {valid file handle}
       else
         {Open error: FileHandle = negative DOS error code}
    end;
    范例
    procedure TForm1.Button1Click(Sender: TObject);
    var
       iFileHandle: Integer;
       iFileLength: Integer;
       iBytesRead: Integer;
       Buffer: PChar;
       i: Integer
    begin
       if OpenDialog1.Execute then
       begin
         try
           iFileHandle := FileOpen(OpenDialog1.FileName, fmOpenRead);
           iFileLength := FileSeek(iFileHandle,0,2);
           FileSeek(iFileHandle,0,0);
           Buffer := PChar(AllocMem(iFileLength + 1));
           iBytesRead = FileRead(iFileHandle, Buffer, iFileLength);
           Fil 
    eClose(iFileHandle);
           for i := 0 to iBytesRead-1 do
           begin
             StringGrid1.RowCount := StringGrid1.RowCount + 1;
             StringGrid1.Cells[1,i+1] := Buffer[i];
             StringGrid1.Cells[2,i+1] := IntToStr(Integer(Buffer[i]));
           end;
         finally
           FreeMem(Buffer);
         end;
       end;
    end;
    ##FileOpen, FileSeek, FileRead Example
    -----------------------------------------------------------------------------
    FileCreate    建档
    -----------------------------------------------------------------------------
    Unit   SysUtils
    函数原型 function FileCreate(const FileName: string): Integer;范例
    procedure TForm1.Button1Click(Sender: TObject);
    var
       BackupName: string;
       FileHandle: Integer;
       StringLen: Integer;
       X: Integer;
       Y: Integer;
    begin
       if SaveDialog1.Execute then
       begin
         if FileExists(SaveDialog1.FileName) then
         begin
           BackupName := ExtractFileName(SaveDialog1.FileName);
           BackupName := ChangeFileExt(BackupName, '.BAK');
           if not RenameFile(SaveDialog1.FileName, BackupName) then         raise Exception.Create('Unable to create backup file.');
         end;
         FileHandle := FileCreate(SaveDialog1.FileName);
         { Write out the number of rows and columns in the grid. }
         FileWrite(FileHandle, 
           StringGrid1.ColCount, SizeOf(StringGrid1.ColCount));
         FileWrite(FileHandle, 
           StringGrid1.RowCount, SizeOf(StringGrid1.RowCount));
         for X := 0 to StringGrid1.ColCount ? 1 do
         begin       for Y := 0 to StringGrid1.RowCount ? 1 do
           begin
             { Write out the length of each string, followed by the string itself. }
             StringLen := Length(StringGrid1.Cells[X,Y]);
             FileWrite(FileHandle, StringLen, SizeOf(StringLen));
             FileWrite(FileHandle,
               StringGrid1.Cells[X,Y], Length(StringGrid1.Cells[X,Y]);
           end;
         end;
         FileClose(FileHandle);
       end;end;
    ##FileExists, RenameFile, FileCreate, FileWrite, FileClose, ExtractFileName Example
    -----------------------------------------------------------------------------
    FileClose    关档
    -----------------------------------------------------------------------------
    Unit   SysUtils
    函数原型 procedure FileClose(Handle: Integer);
    范例
    procedure TForm1.Button1Click(Sender: TObject);
    var
       BackupName: string;
       FileHandle: Integer;
       StringLen: Integer;
       X: Integer;
       Y: Integer;
    begin
       if SaveDialog1.Execute then
       begin
         if FileExists(SaveDialog1.FileName) then
         begin
           BackupName := ExtractFileName(SaveDialog1.FileName);
           BackupName := ChangeFileExt(BackupName, '.BAK');
           if not RenameFile(SaveDialog1.FileName, BackupName) then
             raise Exception.Create('Unable to create backup file.');
         end;
         FileHandl  
    e := FileCreate(SaveDialog1.FileName);
         { Write out the number of rows and columns in the grid. }
         FileWrite(FileHandle, 
           StringGrid1.ColCount, SizeOf(StringGrid1.ColCount));
         FileWrite(FileHandle, 
           StringGrid1.RowCount, SizeOf(StringGrid1.RowCount));
         for X := 0 to StringGrid1.ColCount ? 1 do
         begin
           for Y := 0 to StringGrid1.RowCount ? 1 do
           begin
             { Write out the length of each string, followed by the string itself. }
             StringLen := Length(StringGrid1.Cells[X,Y]);
             FileWrite(FileHandle, StringLen, SizeOf(StringLen));
             FileWrite(FileHandle,
               StringGrid1.Cells[X,Y], Length(StringGrid1.Cells[X,Y]);
           end;
         end;
         FileClose(FileHandle);
       end;
    end;
    ##FileExists, RenameFile, FileCreate, FileWrite, FileClose, ExtractFileName Example============================================
    ****   它是以Handle为叁数.
    ============================================
    FileRead    读取档案
    -----------------------------------------------------------------------------
    Unit   SysUtils
    函数原型 function FileRead(Handle: Integer; var Buffer; Count: Integer):Integer;
    范例
    procedure TForm1.Button1Click(Sender: TObject);var
       iFileHandle: Integer;
       iFileLength: Integer;
       iBytesRead: Integer;
       Buffer: PChar;
       i: Integer
    begin
       if OpenDialog1.Execute then
       begin
         try
           iFileHandle := FileOpen(OpenDialog1.FileName, fmOpenRead);
           iFileLength := FileSeek(iFileHandle,0,2);
           FileSeek(iFileHandle,0,0);
           Buffer := PChar(AllocMem(iFileLength + 1));
           iBytesRead = FileRead(iFileHandle, Buffer, iFileLength);
           FileClose(iFileHandle);
           for i := 0 to iBytesRead-1 do
           begin
             StringGrid1.RowCount := StringGrid1.RowCount + 1;
             StringGrid1.Cells[1,i+1] := Buffer[i];
             StringGrid1.Cells[2,i+1] := IntToStr(Integer(Buffer[i]));
           end;
         finally
           FreeMem(Buffer);
         end;
       end;
    end;
    ##FileOpen, FileSeek, FileRead Example
      

  2.   

    FileWrite    写入档案
    -----------------------------------------------------------------------------
    Unit   SysUtils
    函数原型 function FileWrite(Handle: Integer; const Buffer; Count: Integer): Integer;
    范例
    procedure TForm1.Button1Click(Sender: TObject);
    var
       BackupName: string;
       FileHandle: Integer;
       StringLen: Integer;
       X: Integer;
       Y: Integer;
    begin
       if SaveDialog1.Execute then
       begin
         if FileExists(SaveDialog1.FileName) then
         begin
           BackupName := ExtractFileName(SaveDialog1.FileName);
           BackupName := ChangeFileExt(BackupName, '.BAK');
           if not RenameFile(SaveDialog1.FileName, BackupName) then
             raise Exception.Create('Unable to create backup file.');
    end;
         FileHandle := FileCreate(SaveDialog1.FileName);
         { Write out the number of rows and columns in the grid. }
         FileWrite(FileHandle, 
           StringGrid1.ColCount, SizeOf(StringGrid1.ColCount));
         FileWrite(FileHandle, 
           StringGrid1.RowCount, SizeOf(StringGrid1.RowCount));
         for X := 0 to StringGrid1.ColCount   do
         begin
           for Y := 0 to StringGrid1.RowCount do
           begin
             { Write out the length of each string, followed by the string itself. }
             StringLen := Length(StringGrid1.Cells[X,Y]);
             FileWrite(FileHandle, StringLen, SizeOf(StringLen));
             FileWrite(FileHandle,
               StringGrid1.Cells[X,Y], Length(StringGrid1.Cells[X,Y]);//?????????/
           end;
         end;
         FileClose(FileHandle);
       end;
    end;
    ##FileExists, RenameFile, FileCreate, FileWrite, FileClose, ExtractFileName Example
    -----------------------------------------------------------------------------
    FileSeek    移动档案指标位置
    -----------------------------------------------------------------------------
    Unit   SysUtils
    函数原型 function FileSeek(Handle, Offset, Origin: Integer): Integer;
    说明   Origin=0读/写指标由档案开头算起.
        Origin=1读/写指标由目前位置算起.
        Origin=2读/写指标移动到档案结束处.
    ****   功能与Dos Int 21h 插断 42h 的功能相同.
        失败传回-1.
    范例   procedure TForm1.Button1Click(Sender: TObject);
        var
          FileHandle : Integer;
          FileName : String;
          Buffer   : PChar;
          S    : String;
          ReadBytes : Integer;
        begin
          FileName:='c:\delphi_test\abc.ttt';
          S:='1234567890';
          if FileExists(FileName) then
         FileHandle := FileOpen(FileName, fmOpenReadWrite)
          else
         FileHandle := FileCreate(FileName);
          if FileHandle < 0 then
         Begin
          MessageDlg('开档失败', mtInformation, [mbOk], 0);
          Exit;
         End;      GetMem(Buffer, 100);
          try
         StrPCopy(Buffer, S);
         FileWrite(FileHandle,Buffer^,10);
         FileSeek(FileHandle,4,0);
         ReadBytes:=FileRead(FileHandle, Buffer^, 100);
         Buffer[ReadBytes]:=#0;
         Label1.Caption:=IntToStr(ReadBytes)+'    '+
          StrPas(Buffer);
          finally
         FreeMem(Buffer);
          end;      FileClose(FileHandle);
        end;结果   存档後abc.ttt共有1234567890等十个Bytes.
        从第五位元开始读取,共读取六个位元.
        567890
        (位移是从0开始算起)procedure TForm1.Button1Click(Sender: TObject);var
       iFileHandle: Integer;
       iFileLength: Integer;
       iBytesRead: Integer;
       Buffer: PChar;
       i: Integer
    begin
       if OpenDialog1.Execute then
       begin
         try
           iFileHandle := FileOpen(OpenDialog1.FileName, fmOpenRead);
           iFileLength := FileSeek(iFileHandle,0,2);
           FileSeek(iFileHandle,0,0);
    Buffer := PChar(AllocMem(iFileLength + 1));
           iBytesRead = FileRead(iFileHandle, Buffer, iFileLength);
           FileClose(iFileHandle);
           for i := 0 to iBytesRead-1 do
           begin
             StringGrid1.RowCount := StringGrid1.RowCount + 1;
             StringGrid1.Cells[1,i+1] := Buffer[i];
             StringGrid1.Cells[2,i+1] := IntToStr(Integer(Buffer[i]));
           end;
         finally
           FreeMem(Buffer);
         end;
       end;
    end;
    ##FileOpen, FileSeek, FileRead Example
    -----------------------------------------------------------------------------
    FileGetAttr   档案属性
    -----------------------------------------------------------------------------
    Unit   SysUtils
    函数原型 function FileGetAttr(const FileName: string): Integer;
    说明   faReadOnly = $00000001;
        faHidden   = $00000002;
        faSysFile   = $00000004;
        faVolumeID = $00000008;
        faDirectory = $00000010;
        faArchive = $00000020;
        faAnyFile = $0000003F;
    范例   procedure TForm1.Button1Click(Sender: TObject);
        var
          S: String;
        begin
          S:=IntToStr(FileGetAttr('c:\delphi_d\delphi_help1.txt'));
          Label1.Caption := S;
        end;
    -----------------------------------------------------------------------------
    FileSetAttr    设定档案属性
    -----------------------------------------------------------------------------
    Unit   SysUtils
    函数原型 function FileSetAttr(const FileName: string; Attr: Integer): 
         Integer;
    说明   设定成功传回0
    -----------------------------------------------------------------------------
    FindClose    结束FindFirst/FindNext
    -----------------------------------------------------------------------------
    procedure TForm1.Button1Click(Sender: TObject);var
       sr: TSearchRec;
       FileAttrs: Integer;
    begin
       StringGrid1.RowCount := 1;
       if CheckBox1.Checked then
         FileAttrs := faReadOnly
       else
         FileAttrs := 0;
       if CheckBox2.Checked then
         FileAttrs := FileAttrs + faHidden;
       if CheckBox3.Checked then
         FileAttrs := FileAttrs + faSysFile;
       if CheckBox4.Checked then
         FileAttrs := FileAttrs + faVolumeID;
       if CheckBox5.Checked then     FileAttrs := FileAttrs + faDirectory;
       if CheckBox6.Checked then
         FileAttrs := FileAttrs + faArchive;
       if CheckBox7.Checked then     FileAttrs := FileAttrs + faAnyFile;   if FindFirst(Edit1.Text, FileAttrs, sr) = 0 then   begin
         with StringGrid1 do
         begin
           if (sr.Attr and FileAttrs) = sr.Attr then
           begin
             Cells[1,RowCount-1] := sr.Name;
             Cells[2,RowCount-1] := IntToStr(sr.Size);
           end;
           while FindNext(sr) = 0 do
           begin
             if (sr.Attr and FileAttrs) = sr.Attr then
             begin
             RowCount := RowCount + 1;
             Cells[1, RowCount-1] := sr.Name;   Cells[2, RowCount-1] := IntToStr(sr.Size);
             end;
           end;
           FindClose(sr);
         end;
       end;
    end;
    ##FindFirst, FindNext, FindClose Example
      

  3.   

    其实不用贴这么长的代码就是一句话
    fmopenreadandwrite是说当你打开这个文件时允许进行读写操作,
    fmopenwrite是说,只写方式打开,你打开后不予许其他程序再读取该文件
    fmopenread是只读模式,只允许读取,不允许修改
      

  4.   

    你希望自己在写文件的时候,文件被其他程序修改么?使用fmopenread吧
      

  5.   

    又打错了写文件时是fmopenwrite,今天睡眠不足脑残了
      

  6.   

    fmOpenReadWrite or fmShareDenyWrite
    我打开文件的时候,用的标志如上。但是有时候还是会出现:“ 另一个程序正在使用此文件,进程无法访问”。写文件我是专门放在一个类里面进行管理的,不应该出现文件访问冲突才对啊。
      

  7.   

    fmOpenRead :以只读方式打开指定文件 
    fmOpenWrite :以只写方式打开指定文件 
    fmOpenReadWrite:以写写方式打开指定文件 
    共享模式: fmShareExclusive:不允许别的程序以任何方式打开该文件 
    fmShareDenyWrite:不允许别的程序以写方式打开该文件 
    fmShareDenyRead :不允许别的程序以读方式打开该文件 
    fmShareDenyNone :别的程序可以以任何方式打开该文件 
      

  8.   

    唉~一段时便不用delphi退化了,刚才查了下帮助,8好意思
      

  9.   

    fmShareExclusive没错的,做不做线程保护这个文件都不会被其他程序访问,除非在你打开前已经被别的程序使用,那你打开会报错