Remember to add Classes to your uses clause to use Tfilestream.
Creation:
[i]
var
FileS: TFilestream;
begin
FileS := TFilestream.Create('C: estfile.txt',fmOpenRead,fmShareDenyWrite);
try
{code goes here}
finally
FileS.Free;
end;
end;
[/i]
This shows you how to create a Filestream. The first parameter defines the filename. Here i have just used a sample text file. The next parameter is the file mode. Where I have opened for reading only. The possible values are:
fmCreate - Create a file, open in write mode if exists.
fmOpenRead - Open for just reading
fmOpenWrite - Open for just
fmOpenReadWrite - Open for both reading and writing.
A third parameter is also possible to add, although you do not have to add it. This is the Share mode parameter. It tells what right other applications have to use the same file you are using. The possible values are:
fmShareCompat - Sharing is compatible with the way FCBs are opened.fmShareExclusive - Other apps may not open this file.
fmShareDenyWrite - Other apps may not write to this file.
fmShareDenyRead - Other apps may not Read from this file
fmShareDenyNone - The file is open for any use from other apps.
you should always use try .. finally .. end; to make sure that your Filestream is freed, no matter what happens.
Reading:
[i]var
FileS: TFilestream;
I: Integer;
Buff: Byte;
Teststring: String;
begin
FileS := TFilestream.Create('C: estfile.txt',fmOpenRead);
try
for I := 0 to FileS.Size do
begin
FileS.Read(Buff,1);
Teststring := Teststring + Chr(Buff);
end;
finally
FileS.Free;
end;
end;
[/i]
This shows a basic usage of the filestream. It goes through every byte in the textfile, and appends them to a string. Although this doesn't do anything it shows how to read. The first parameter tells the filestream what buffer to read to. Here we have used a single byte. The second parameter tells the filestream how many bytes to read. Here we have to use 1 because we're only reading one byte at a time.If you want to read several bytes at a time you have to use an array of bytes.
Writing:
[i]var
FileS: TFilestream;
I: Integer;
Buff: Byte;
begin
FileS := TFilestream.Create('C: estfile.txt',fmCreate);
try
for I := 0 to 299 do
begin
Byte := Random(255);
FileS.Write(Buff,1);
end;
finally
FileS.Free;
end;
end;
[/i]
This example shows how to write to a file. It creates C: estfile.txt and writes 300 characters of random information. The first parameter in the Write function is the buffer to write from. The second tells how many bytes to write. And as in reading, to write several bytes at a time you have to use an array of bytes.
I have given you a few examples for using TFilestream. This should hold for a lot of tasks involving files. You don't have to use Byte, you can also use other buffer types. String, Integer and so on (I haven't tried this yet though). Remember that the Write and Read functions return how many bytes that have actually been read, as it isn't always possible to read the amount you have requested in the second parameter. For more information and definitions, consult the Delphi help file.
SkyFex (skyfex@hotmail.com)
This article was originally written by skyfex |