Solved

Write and Read File - How do I?

Posted on 2000-03-11
8
140 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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

829 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