Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Write and Read File - How do I?

Posted on 2000-03-11
8
Medium Priority
?
144 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 800 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

618 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