Solved

Write and Read File - How do I?

Posted on 2000-03-11
8
135 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
 
LVL 9

Expert Comment

by:ITugay
ID: 2607443
to rwilson.

You will? Or I can.

Best regards,
Igor.

0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now