Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Actually, I prefer this:</p> <pre><code>var Txt: TextFile; SomeFloatNumber: Double; SomeStringVariable: string; Buffer: Array[1..4096] of byte; begin SomeStringVariable := 'Text'; AssignFile(Txt, 'Some.txt'); Rewrite(Txt); SetTextBuf(Txt, Buffer, SizeOf(Buffer)); try WriteLn(Txt, 'Hello, World.'); WriteLn(Txt, SomeStringVariable); SomeFloatNumber := 3.1415; WriteLn(Txt, SomeFloatNumber:0:2); // Will save 3.14 finally CloseFile(Txt); end; end; </code></pre> <p>I consider this the easiest way, since you don't need the classes or any other unit for this code. And it works for all Delphi versions including -if I'm not mistaken- all .NET versions of Delphi...</p> <hr> <p>I've added a call to SetTextBuf() to this example, which is a good trick to speed up textfiles in Delphi considerably. Normally, textfiles have a buffer of only 128 bytes. I tend to increase this buffer to a multiple of 4096 bytes. In several cases, I'va also implemented my own TextFile types, allowing me to use these "console" functions to write text to memo fields or even to another, external application! At <a href="http://wimtenbrink.nl/Textfiles.zip" rel="nofollow noreferrer">this location</a> is some example code (ZIP) I wrote in 2000 and just modified to make sure it compiles with Delphi 2007. Not sure about newer Delphi versions, though. Then again, this code is 10 years old already.<br />These console functions have been a standard of the Pascal language since it's beginning so I don't expect them to disappear anytime soon. The TtextRec type might be modified in the future, though, so I can't predict if this code will work in the future... Some explanations:</p> <ul> <li>WA_TextCustomEdit.AssignCustomEdit allows text to be written to CustomEdit-based objects like TMemo.</li> <li>WA_TextDevice.TWATextDevice is a class that can be dropped on a form, which contains events where you can do something with the data written.</li> <li>WA_TextLog.AssignLog is used by me to add timestamps to every line of text.</li> <li>WA_TextNull.AssignNull is basically a dummy text device. It just discards anything you write to it.</li> <li>WA_TextStream.AssignStream writes text to any TStream object, including memory streams, file streams, TCP/IP streams and whatever else you have.</li> </ul> <p>Code in link is hereby licensed as <a href="http://creativecommons.org/licenses/by/3.0/" rel="nofollow noreferrer">CC-BY</a> <img src="https://i.stack.imgur.com/gLygg.png" alt="alt text"></p> <hr> <p>Oh, the server with the ZIP file isn't very powerful, so it tends to be down a few times every day. Sorry about that.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload