Solved

Saving the contents of Edit1.Text to File

Posted on 1998-08-27
4
395 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
build html 5 client that connect to indy Tidtcpserver 3 355
How to fill array with TArray.Create? 14 79
Magic Software info 18 125
Delphi 2 49
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

895 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

12 Experts available now in Live!

Get 1:1 Help Now