We help IT Professionals succeed at work.

Difference between TFilestream and file

wimhhf
wimhhf asked
on
Medium Priority
575 Views
Last Modified: 2010-05-18
Hello,

In Delphi you've got different file types:

* Text files (var F: Textfile)
* Typed files (var F: file of ...)
* Untyped files (var F: file)
* Filestreams

What is the difference between Untyped files and Filestreams?
And when do you use untyped files and when filestreams?

Greetings,

Wim
Comment
Watch Question

Commented:
FileStreams is an Object oriented method of accessing any files, specially Untyped Files, it is supported by many Components and Objects such as:

1. Memo : Memo1.Lines.LoadFromStream..
2. ListBoxes
3. TStringList

and many others, also it has a very powerfull methods and properties, for example you can copy from stream to the same kind or different kind such as:

var
  F1, F2: TFileStream;
  M: TMemoryStream;
begin
  F1:= TFileStream.Create('source.txt', fmOpenRead);
  F2:= TFileStream.Create('Target.txt', fmCreate);
  F1.Position:= 0;
  F2.CopyFrom(F1, 0); // Copy all file content, very simple
  M:= TMemoryStream.Create;
  F1.Position:= 0;
  M.CopyFrom(F1);
  // ......
  F1.Free;
  F2.Free;
  M1.Free;
end;

Commented:
FileStreams is an Object oriented method of accessing any files, specially Untyped Files, it is supported by many Components and Objects such as:

1. Memo : Memo1.Lines.LoadFromStream..
2. ListBoxes
3. TStringList

and many others, also it has a very powerfull methods and properties, for example you can copy from stream to the same kind or different kind such as:

var
  F1, F2: TFileStream;
  M: TMemoryStream;
begin
  F1:= TFileStream.Create('source.txt', fmOpenRead);
  F2:= TFileStream.Create('Target.txt', fmCreate);
  F1.Position:= 0;
  F2.CopyFrom(F1, 0); // Copy all file content, very simple
  M:= TMemoryStream.Create;
  F1.Position:= 0;
  M.CopyFrom(F1);
  // ......
  F1.Free;
  F2.Free;
  M1.Free;
end;

Author

Commented:
Are untyped files derived from Filestreams?

Commented:
Also it supports file sharing:

F:= TFileStream.Create('test.txt', fmOpenRead + fmShareDenyWrite);

Then many people can open this file for read only at the same time, and no Access denied will occured in such type of files.

Motaz

Commented:
No, Untyped files has different functionality than TFileStream.

I'll write a table to you to compare the functionality of both files
Commented:
Function                Untyped File                     File Stream
-------------------------------------------------------------------------------------------------------
Declaration          F: File;               F: TFileStream;
Assign file             AssignFile(F, 'test.txt')       F:= TFileStream.Create('test.txt', fmOpenRead);
Open for read           FileMode:= 0; Reset(F);         F:= TFileStream.Create('test.txt', fmOpenRead);
Open for write          FileMode:= 1; Rewrite(F);       F:= TFileStream.Create('test.txt', fmOpenWrite);
Open for read/write     FileMode:= 2; Reset(F);         F:= TFileStream.Create('test.txt', fmOpenRead + fmOpenWrite);
Read data               BlockRead(F, Buf, SizeOf(Buf), NumRead)    NumRead:= F.Read(Buf, SizeOf(Buf));
Seek certain position   Seek(F, 100);                   F.Position:= 100;
Get current pos         X:= FilePos(F);               X:= F.Position;
Get file size          Size:= FileSize(F);          Size:= F.Size;
Close file             CloseFile(F);               F.Free;
---------------------------------------------------------------------------------------------------------

This is some comparison.

The concolution are:

- Untyped provide structured access to files
- File Streams provide Object Oriented method for accessing files
- We can access the same files using both methods

Motaz

Commented:
I think think there's also the THandlestream which is
similar to TFilestream except you have a handle for use
in API calls.

TFilestream is much more flexible, not only can it be
passed to various objects, like Motaz said, you can
descend your own classes from it and do more specialized file I/O.

Use TFileStream when you wanna read data into objects/components and when you wanna write objects/components to a file (TFilestream.WriteComponent).

Untyped files are have much fewer features. You can
basically read/write stuff from a variable or buffer.

Also, I find larger files are much easier to work with
using filestreams.
Untyped files are suited to raw, contiguous data - so you
probably wouldn't wanna use them for say, writing a number
of variable-length chunks of data etc.

Author

Commented:
Thanks for the answer....
Igor UL7AAjrSenior developer

Commented:
Hi wimhhf,

Motaz :-)

Just to add.

TFileStream designed to provide comatibility with Delphi's streaming mechanizm. Streaming mechanizm invented to allow store/load something without care about back-end media.

Using TFileStream is preferable programming style.

-----
Igor.