zoukankan      html  css  js  c++  java
  • delphi的TFileStream

    delphi的TFileStream

    一、文件

    文本文件是以行为单位进行读、写操作的。文本文件只能单独为读或写而打开,在一个打开的文本文件上同时进行读、写操作是不允许的。

    二、定义

    FileStream: TFileStream;

    三、打开文件

    Filestream:= TFileStream.Create(AFileName: string; Mode: Word);

    参数AfileName:文件名;参数Mode:文件打开的方式。

    Mode由打开模式与共享模式组成,取值见下表:

    分类

    参数

    说明

    fmCreate

    建立文件, 如果指定文件名的文件已经存在,则以写模式打开

    fmOpenRead

    只读打开

    fmOpenWrite

    以写模式打开文件,写到文件的内容将替换文件以前的内容

    fmOpenReadWrite

    读写打开

    fmShareCompat

    共享模式, 兼容 Dos

    fmShareExclusive

    他的应用程序不能打开该文件

    fmShareDenyWrite

    其他的应用程序只能以只写方式打开

    fmShareDenyRead

    其他的应用程序只能以只读方式打开

    fmShareDenyNone

    其他的应用程序可以以任何方式打开文件

     

    四、读写文件

    function read(var buffer;count:longint):longint; //从文件流当前位置读count字节到缓冲区BUFFER;

    function write(const buffer;count:longint):longint; //将缓冲区BUFFER的Count个字节的数据写到文件流当前位置中,覆盖该位置后面的Count个字节的数据;

    function seek(offset:longint;origin:word):longint; //设置文件流当前读写指针位置,origin={soFromBeginning,soFromCurrent,soFromEnd}

    function copyfrom(source:TStream;count:longint):longint; //从另一文件流中当前位置复制COUNT到当前文件流当前位置;

    八、关闭文件

    文件的关闭须调用FreeAndNil(FileStream)。

     

     

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    一个实例

    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    type

    TBuffer = array [0..16000]of char

    procedure GetMessageFromDir( AFileName: string; var ABuffer: TBuffer);

    var

         FileStream: TFileStream;

    begin

           Filestream:=TFileStream.Create(AFileName,fmShareExclusive);

           Filestream.Position:=0;

           FileStream.Read(ABuffer,sizeof(ABuffer));

         FreeAndNil(FileStream);

    end;

    procedure PutMessageToDir(AFileName:string; Astr :string);

    var

         FileStream: TFileStream;

         tempBuffer:TBuffer;

    begin

         StrPcopy(tempBuffer,Astr);

         Filestream:=TFileStream.Create(AFileName,fmShareExclusive or fmCreate);

         FileStream.Position:=0;

         FileStream.Write(tempBuffer,length(AStr));

         FreeAndNil(FileStream);

    end;

     

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    一个实例

    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    procedure TForm1.Button1Click(Sender: TObject);

    var

    getStream,setStream: TFileStream; {声明一个文件流}

    getPath,setPath: string;

    begin

    getPath := 'c: empget.jpg'; {这个文件存在}

    setPath := 'c: empset.jpg'; {这个会自动建立}

    getStream := TFileStream.Create(getPath, fmOpenRead or fmShareExclusive);

    setStream := TFileStream.Create(setPath, fmCreate);

    getStream.Position := 0; {流指针移到开始, 复制时从这里开始}

    setStream.CopyFrom(getStream, getStream.Size); {Copy 流}

    {CopyFrom 的第二个参数是要复制的内容大小; 如果为 0 , 不管指针在什么位置都会复制所有内容}

    getStream.Free;

    setStream.Free;

    end;

     

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    一个实例:读取流中的图片数据,显示图片

    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    type

    TForm1 = class(TForm)

        img1: TImage;

        procedure FormShow(Sender: TObject);

    private

        { Private declarations }

    public

        { Public declarations }

        fstream:TFileStream;

    end;

     

    var

    Form1: TForm1;

    filename:string = '.WeiMir.uib';

    implementation

     

    {$R *.dfm}

     

    procedure TForm1.FormShow(Sender: TObject);

    var

    bmp:Tbitmap;

    begin

    fstream:=Tfilestream.create(filename,fmOpenRead);

    fstream.Seek(0,soFromBeginning);

    bmp:=TBitmap.Create;

    bmp.LoadFromStream(fstream);

    img1.Picture.Bitmap:=bmp;

    end;

    end.

  • 相关阅读:
    HDU4565
    CF861D
    UVA 11651
    HDU5950
    POJ3267
    POJ1094
    POJ1905
    HDU3567
    进程的同步与互斥
    预防死锁,检测死锁,避免死锁,解除死锁....
  • 原文地址:https://www.cnblogs.com/martian6125/p/9631050.html
Copyright © 2011-2022 走看看