Solved

Saving the contents of Edit1.Text to File

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

Technology Partners: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi XE10 Round Image 2 193
Magic Software info 18 144
Dev Express grid collapse 2 48
can't find the executable in Simulator 1 112
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

730 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