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

x
?
Solved

Saving the contents of Edit1.Text to File

Posted on 1998-08-27
4
Medium Priority
?
429 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 150 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
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

609 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