Solved

Saving the contents of Edit1.Text to File

Posted on 1998-08-27
4
415 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
[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
  • 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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

717 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