Solved

Saving the contents of Edit1.Text to File

Posted on 1998-08-27
4
386 Views
Last Modified: 2010-04-04
Hi, people!
I'm trying to save the contents of a EditBox (or Memo) to a file using TFileStream, but don't work. Example:

...
var
  Cool: String;
  Arq: TFileStream;
begin
  Arq := TFileStream.Create ('test.dat', fmCreate);
  Cool := Edit1.Text;
  Arq.Write (Cool, Length(Cool));
  Arq.Free;
end;

  Well, when I open that file, there's not my text on it, just weird characters. How can I work it out?

  Any king of help will be great!
  Thank's a lot!
0
Comment
Question by:joaquim
  • 3
4 Comments
 
LVL 2

Accepted Solution

by:
kjteng earned 50 total points
ID: 1337952
I tried your code - It works except that the last character is not saved and the first character is a byte showing the length of the string. The reason is obvious: The first character of cool is actually the byte used to indicate its length.

Try amend your code to:
var
     Cool: array[0..255] of char;
     Arq: TFileStream;
   begin
     Arq := TFileStream.Create ('test.dat', fmCreate);
     StrPCopy(Cool, Edit1.Text);
     Arq.Write (Cool, Length( Edit1.Text));
     Arq.Free;
   end;


If you are using TMemo, just call the method SaveToFile.
0
 
LVL 2

Expert Comment

by:kjteng
ID: 1337953
Hi there, does it work?
0
 

Author Comment

by:joaquim
ID: 1337954
HI! That's work, but I think that this is not the best way, because de Memo (could be 65000 or more characteres, and creating a var test: array[0..65535] of char will eat a lot of memory). I browse the Delphi manual and I found a great 'trick' to do this. Look:

var
  Cool: String;
  Arq: TFileStream;
begin
  Cool := Edit1.Text;
  Arq := TFileStream.Create ('foo.dat', fmCreate);
  Arq.Write (PChar(Cool)^, Length(Cool));
  Arq.Close;
end;

  That's work very well.
 
  But, with the code I wrote before, nothing but garbage was written to the file. How this works with you?

  Anyway, thanks a lot for your help!
0
 
LVL 2

Expert Comment

by:kjteng
ID: 1337955
Your code work because I have change the variable Cool to zero based array of char (which is actually a pchar). The rest of the algo is basically the same as what you found in the Delphi manual.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

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…
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…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
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

14 Experts available now in Live!

Get 1:1 Help Now