Solved

Write and Read File - How do I?

Posted on 2000-03-11
8
142 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
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 this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

691 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