Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to load a file into a TMemoryStream and set the size of a string to contain Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
18-Oct-02
Category
Files Operation
Language
Delphi 2.x
Views
85
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Jonas Bilinkevicius

I've come across a problem with my file type. I have a record which has an array of 
bytes. However, the size of this array varies, or can vary depending how big the 
array needs to be. How can I load the record knowing the size of the array?

Answer:

Here's a code fragment that shows how to load a file into a TMemoryStream and then 
set the size of a string (an array element in the example below) to contain the 
whole file. The variable s[i] then contains all the bytes of the file:
1   
2   procedure TForm1.ButtonCombineClick(Sender: TObject);
3   var
4     i: Integer;
5     s: array[1..5] of string;
6     size: Integer;
7     Stream: TMemoryStream;
8   begin
9     for i := Low(FileList) to High(FileList) do
10    begin
11      {Load files into strings}
12      if FileExists(FileList[i]) then
13      begin
14        Stream := TMemoryStream.Create;
15        try
16          Stream.LoadFromFile(FileList[i]);
17          SetLength(s[i], Stream.Size);
18          Stream.read(s[i][1], Stream.Size)
19        finally
20          Stream.Free
21        end
22      end
23      else
24        s[i] := '';
25    end;
26  end;
27  { ... }


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC