Delphi Files, Directories, Disks, I/O
A. Files B. Directories C. Disks/Drives D. File I/O E. Stream I/O F. Port I/O

(bookmark.gif (183 bytes) Bookmark)

general

Chapter 6, Streaming
Delphi Component Design by Danny Thorpe

File Open Mode constants Use the file open mode constants to control the shareability
of a file or stream when it's opened.
fmOpenRead $0000 Open for read access only (SysUtils Unit)
fmOpenWrite $0001 Open for write access only
fmOpenReadWrite $0002 Open for read and write access
fmShareCompat $0000 Compatible with the way FCBs are opened
fmShareExclusive $0010 Open the file, disallowing other applications to open it for reading or writing.
fmShareDenyWrite $0020 Open the file, disallowing other applications to open it for writing.

Philippe Ranger's UseNet Post with File Locking Example

fmShareDenyRead $0030 Open the file, disallowing other applications to open it for reading
fmShareDenyNone $0040 Open the file, disallowing other applications to open it for exclusive use.
     
fmCreate $FFFF TFileStream create mode (classes Unit)
How to store records to stream ... and retrieve them later
www.rksolution.cz/delphi/dtip0003.txt
LoadFromStream Steve Schafer's UseNet Post with procedures SaveGraphicToStream and LoadGraphicFromStream
SaveToStream Steve Schafer's UseNet Post with procedures SaveGraphicToStream and LoadGraphicFromStream
Seek Classes Unit:
soFromBeginning = 0
soFromCurrent   = 1
soFromEnd       = 2

- YUV Lab Reports (see YVU-9/YVU-12 example for simultaneously reading three file streams from same file after seeking to different start points)

TFiler How to save/ load any TPicture-contained TGraphic to/ from a stream
www.lmc-mediaagentur.de/dpool/tips04/0367.htm 
bookmark.gif (183 bytes)TFileStream Example:
  VAR
    FileName  :  STRING;
    FileStream:  TFileStream;
    s         :  STRING;
  . . .
  FileName := ArticleDirectory + '\' +
              ArticleFilename  + '.TXT';
  FileStream := TFileStream.Create(FileName,
                                   fmCreate or fmOpenWrite or fmShareExclusive);
  TRY
    s := NMNNTP.Header.Text;
    FileStream.Write(s[1], LENGTH(s));
    s := NMNNTP.Body.Text;
    FileStream.Write(s[1], LENGTH(s))
  FINALLY
    FileStream.Free
  END;

 

efg's Combine files/Split file example.  Combine five given files, 1.DAT, ..., 5.DAT into a single COMBINED.DAT file. Split the COMBINED.DAT file back into the original files, but with a new extension, 1.BIN, 2.BIN, ...

efg's Combine JPGs, split and display example.  Combine files 1.JPG, 2.JPG, 3.JPG into a single file.  Read the combined file and display the original JPGs in TImages.

efg's Resource Demo application shows how to use the following file types in resource files:  BMP, ICO, JPG, GIF (requires Anders Melander's TGIFImage), EMF, TXT, AVI, WAV, cursors, and a matrix of binary data (Integers and Doubles).  Requires D4 (or above) for the dynamic array of binary data.  The 2D dynamic array is written to a file using a TFileStream.

Also see:
- Chris Willig's  UseNet Post with example of reading file in chunks
- efg's JPEGSentinelsAreOK function to find "bad" JPEGs (avoid error #52) -- verifies first two and last two bytes of JPEG are correct
- Peter Below's UseNet Post about sharing files on a network
- Some Remarks on TFileStream, www.undu.com/Articles/000928f.html 
- "Delphi Import/Export:  Part II: Streams and Bit Fiddling" in Delphi Informant, July 1998.
- YUV Lab Reports (see YVU-9/YVU-12 example for simultaneously reading three file streams from same file)
- Jens Gruschel's UseNet Post about loading a JPEG from a stream
- Deepak Shenoy's UseNet Post with file stream example of converting UNIX text to DOS text
- Example of reading three Sun Raster files as TFileStreams and creating Bitmap, efg's Create Mandrill BMP Lab Report
- Berend's UseNet Post about a CopyFile procedure using TFileStreams
- Three memory mapped file classes: TMapStream, TTextMap, and TRecordMap. Useful when you'd like to use Pascal's file of  record or textfile types, but need memory mapping.  http://delphi.icm.edu.pl/ftp/d20free/mapfiles.zip
- Julian Bucknall got fed up with TFileStream being slow with lots of small data chunks so he set to and wrote a replacement go-faster version with efficient buffering.   Delphi Magazine, Issue 29, January 1998
- Peter Below's UseNet Post with ConcatFiles procedure
- Biff Kadidlehopper's UseNet Post to get/set the dpi in a bitmap file or bitmap image (the biXPelsPerMeter and biYPelsPerMeter values)
- efg's
PelsPerMeter example with GetPelsPerMeter procedure to return biXPelsPerMeter and biYPelsPerMeter values, and an example based on a UseNet Post by Peter Klein that returns these same values.
- Example based on UseNet post by Peter Klein showing how to extract DPI directly from JPG file.

TIFileStream
(IStreams)
 
TIMemoryStream
(IStreams)
 
TIStreamAdapter
(IStreams)
Dan Russell's UseNet Posting about converting TFileStream to IStream
TMapStream Three memory mapped file classes: TMapStream, TTextMap, and TRecordMap. Useful when you'd like to use Pascal's file of record or textfile types, but need memory mapping. Options for mutexes, encryption, & file headers. Heavily commented code, and helpfile included.  By Deborah Pate.
www.djpate.freeserve.co.uk/MapFiles.zip
bookmark.gif (183 bytes)TMemoryStream How to move a string into a TMemoryStream or TFileStream
http://community.borland.com/article/0,1410,26416,00.html 

efg's Combine files/Split file example.  Combine five given files, 1.DAT, ..., 5.DAT into a single COMBINED.DAT file. Split the COMBINED.DAT file back into the original files, but with a new extension, 1.BIN, 2.BIN, ...

Thomas Kowalski's  Drehen90Grad (Rotate bitmap 90 degree) routine in Unit7 shows how to use a memory stream to access the TBitmapInfo header and the pixel data in a bitmap.

TMemoryStream Example:  How to copy the content of a TRichEdit without loosing the RTF format?
originally at www.delphigalaxy.bizland.com/tips/t19.html (but no longer online there):

procedure CopyRTF;
  var MS:  TMemoryStream;
begin
  MS := TMemoryStream.Create;
  try
   RichEdit1.Lines.SaveToStream(MS);
   MemoryStream.Seek(0,soFromBeginning);
   RichEdit2.Lines.LoadFromStream(MS);
  finally
   MS.Free;
  end;
end;

Peter Below's UseNet Post with SaveBitmapToStream and LoadBitmapFromStream examples.
Peter Below's (Team B) Usenet Post with sample code using TMemoryStreams and CompareMem to compare files.

Chris Willig's  UseNet Post with example of reading file in chunks

Ronald Hoek's UseNet Post about playing audio from a TMemoryStream

Example of how to compare two TIcons

efg's UseNet Post about how to flip a TBitmap by writing Scanlines to a MemoryStream and loading the Scanlines of a second TBitmap in the flipped order.

efg's Lead Tools-to-Delphi conversions show how how to start with a hDIB and end up with a TBitmap.  In particular, look at  Method 2, hDIB to TBitmap resulting in a bmDIB using a MemoryStream. 

Bob Villiers' UseNet Post with VarArrayLoadFile, which loads a bitmap into a variant using BlockRead, and then uses LoadFromStream to load into a memory stream.

JPEG File Encryption/Decryption
CryptImage.jpg (948 bytes)   efg's ImageCrypt Lab Report

How to save/ load any TPicture-contained TGraphic to/ from a stream
http://www.lmc-mediaagentur.de/dpool/tips04/0367.htm 

Other Examples:

// Load ASCII file into (long) String
VAR
  CommentFilename       :  STRING;
  Filename              : STRING;
  RecommendationsRemarks: STRING;
  Stream                : TMemoryStream; 
...
CommentFilename := ChangeFileExt(Filename, '.Recommendations');
IF   FileExists(CommentFilename)
THEN BEGIN
  Stream := TMemoryStream.Create;
  TRY
    Stream.LoadFromFile(CommentFilename);
    SetLength(RecommendationsRemarks, Stream.Size);
    Stream.Read(RecommendationsRemarks[1], Stream.Size)
  FINALLY
    Stream.Free
  END;
END;
// Save (long) String to ASCII file
CommentFilename := ChangeFileExt(Filename, '.Recommendations');
DeleteFile(CommentFilename);
IF   LENGTH(RecommendationsRemarks) > 0
THEN BEGIN
  Stream := TMemoryStream.Create;
  TRY
    Stream.Write(RecommendationsRemarks[1],
                 LENGTH(RecommendationsRemarks));
    Stream.SaveToFile(CommentFilename)
  FINALLY
  Stream.Free
  END
END;

TResourceStream

efg's Resource Demo application shows how to use the following file types in resource files:  BMP, ICO, JPG, GIF (requires Anders Melander's TGIFImage), EMF, TXT, AVI, WAV, cursors, and a matrix of binary data (Integers and Doubles).  Requires D4 (or above) for the dynamic array of binary data.
bookmark.gif (183 bytes)TStream Steve Schafer's UseNet Post with procedures SaveGraphicToStream and LoadGraphicFromStream
Steve Schafer's UseNet Post with CopyStreamToFile procedure

Mike Orriss' UseNet Post with CompressStream and ExpandStream procedure using Zlib.

Updated 07 Oct 2007


since 19 Apr 1999