Solved

Write and Read File - How do I?

Posted on 2000-03-11
8
141 Views
Last Modified: 2010-04-04
I want to have App1 load a picture, and also have several edit boxes containing data.  I want App1 (when you press a button) to create a new file containing the picture, and the data entered in the EditBoxes.

I also want App1 to be able to load that file again with the data in it's correct spot and the picture back into the Timage.

I have worked a little with BlockRead and BlockWrite, but I am unsure how to accomplish the task that I am trying to do.

Just as an example: Let's say that I used this App1, loaded my picture, entered a title, year, etc.  Then I hit the button "Create Custom File" and it creates a file with the extension of ".cfl" for Custom File.  Later I wanted to edit that file, so I click on "Load Custom File" and it only shows the .cfl files.  I choose one and load it, and it shows the picture and data.

Please, any comments would be useful.

0
Comment
Question by:perkley
8 Comments
 
LVL 12

Expert Comment

by:rwilson032697
ID: 2607401
You might find it easier to use streams, like this:


// Save the file
var
  s : TFileStream;
  L : Integer;
begin
  s := TFileStream.Create('Fred.cfl', fmCreate);
 
// Write a string to the stream
  L := length(AString);
  S.Write(L, sizeof(L));
  S.Write(AString[1], L);

// Write the bitmap
  Bitmap1.SaveToStream(S);
end;

// Read the file
var
  s : TFileStream;
  L : Integer;
begin
  s := TFileStream.Create('Fred.cfl', fmReadWrite);
 
// Read a string from the stream
  S.Read(L, sizeof(L));
  SetLength(AString, L);
  S.Read(AString[1], L);

// Read the bitmap
  Bitmap1.LoadFromStream(S);
end;

Cheers,

Raymond.
0
 
LVL 9

Expert Comment

by:ITugay
ID: 2607411
You can use TFileStream

// save to file
var F : TFileStream;
begin

   F:=TFileStream.Create('filename.ext',fmCreate);
   Image1.Picture.Bitmap.SaveToStream(F);
   // F.Write(buffer,count); write all what you want here
   F.Free;

end;

// read from file
var F : TFileStream;
begin
   F:=TFileStream.Create('filename.ext',fmOpenRead);
   Image1.Picture.Bitmap.LoadFromStream(F);
   // F.Read(buffer,count); read all what you want here
   F.Free;

end;

Cheers,
Igor.
0
 

Author Comment

by:perkley
ID: 2607435
You both have enlightened me somewhat, but I still am unsure how to read multiple text lines individually.

For example, let's say I have 10 edit boxes with different lengths of data.

How can I write them all, and how can I read them all back to the EditBox they need to be in?  Give code example of putting it back into the EditBox.

This is how it needs to work.  Let's say that I have created 10 different Custom Files.  I choose one, it needs to know all of the information and put it back where it was previously.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Expert Comment

by:ITugay
ID: 2607443
to rwilson.

You will? Or I can.

Best regards,
Igor.

0
 
LVL 20

Expert Comment

by:Madshi
ID: 2607459
Just one little hint to Raymond...  :-)

  L := length(AString);
  S.Write(L, sizeof(L));
  S.Write(AString[1], L);

Better use "pchar(AString)^" instead of "AString[1]". Because in D4&5 (not in D3) you'll get an exception with your code if the string is empty.

Regards, Madshi.
0
 
LVL 9

Accepted Solution

by:
ITugay earned 200 total points
ID: 2607470
There is sample how to write and read TEdit.Text TO and FROM file.

procedure StringToStream(STM : TStream; const S : string);
var LEN : integer;
begin
  LEN:=length(S);
  STM.Write(LEN,SizeOf(LEN));
  STM.Write(pointer(S)^,LEN);
end;

function StringFromStream(STM : TStream) : string;
var LEN : integer;
begin
  STM.Read(LEN,SizeOf(LEN));
  SetLength(result,LEN);
  STM.Read(pointer(result)^,LEN);
end;



// save Edit's text
var F : TFileStream;
begin
    F := TFileStream.Create('C:\000.stm',fmCreate);
    StringToStream(F,Edit1.Text);
    StringToStream(F,Edit2.Text);
    StringToStream(F,Edit3.Text);
    F.Free;
end;

// load Edit's text
var F : TFileStream;
begin
    F := TFileStream.Create('C:\000.stm',fmOpenRead);
    Edit1.Text:=StringFromStream(F);
    Edit2.Text:=StringFromStream(F);
    Edit3.Text:=StringFromStream(F);
    F.Free;
end;

Cheers,
Igor.
0
 

Author Comment

by:perkley
ID: 2607506
Thank you very much, works well, and simpler than I had expected.
0
 
LVL 9

Expert Comment

by:ITugay
ID: 2607512
Always welcome.

Cheers,
Igor.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question