HOME - - - - - Lazarus Tutorials TOC - - - - - - Other material for programmers

Using Filestreams for reading from/ writing to disks, backing store

(URL: lt2Ne-filestreams.htm)

If you want to read something from a disk, or write something to it, filestreams are probably the way to go. They will also be useful with other backing storage, and even with more exotic data sources, destinations.


I started computing before disks were available, even in a school's computer lab. And "grew up" with an older system of backing store i/o. (AssignFile, Reset, Rewrite, etc.) It has taken me a while to shake off my lethargy, get my act together, move to the brave new world of TFilestreams, but, at last, here's a preliminary offering.

The good news is that I've supplied you with sourcecode of a working example.

The bad news is that there's little "hand holding" available for this topic at this time.

I also suspect that I haven't done the best job ever seen of TFilestream based disk I/O... but it if helps you, great, and if not... sorry!

You will find some use of try... except... end, and try... finally blocks. Not every eventuality has been trapped, I suspect, but if you examine the code, maybe it will help you move ahead from where you are now... unless you are already "expert". I hope so.

Two-in-one application

The application supplied with this tutorial demonstrates two things...

First you read a file from the disc into a memo on the application's window. (xxx.Lines.LoadFromFile( is a more straightforward "answer"... in many cases. Mastering the ideas demonstrated here open up new possibilities for you.) (and xxx.Lines.SaveToFile( would be "the way to go" if you really didn't want to do more than the application does.)

Once the file has been moved from disk to memo, the contents of the first memo are moved to a second memo.

Just to keep it all from being totally pointless, a tiny bit of processing is applied... anywhere there was an "n" in the source, you will find there is now an "X".

And then the contents of the second memo are written to the disk, in a file of a different name.

Pretty trivial? Well, yes and no. To do it well is far from trivial. Any time you are writing to a new file, you need to consider the case where someone tries to over-write an existing file. If that's what the user wants, fine. If not, he/ she won't thank you for software that can do it without checking that it was your intention.

The application also makes a lot of use of enabling and disabling ("graying out") controls. (Buttons.)

You can't, for instance, "save the second memo to disk" until there's something in the second memo. To put something in the second memo, you must first fill the first memo. And so on.

As I said... for what the application does, there are simpler answers. But what if the file you were working on was too big to put in a memo?

What if you wanted to....

In other words... how could you, with LoadFromFile, process an input file without ever having all of it in the computer (off the disk) at one time? You couldn't, I don't think.

With Filestreams, you can do that sort of thing... although I haven't done that in the simple demo these notes introduce.

Along the way, the use of the Lazarus (file)Open and (file)Save dialogs get demonstrated.

Sourcecode provided

You can download a .zip file (LDN184.zip) with the example of using Filestreams. It has all the sourcecode, and a copy of the compiled .exe, as well, so you can have a play with that even before you bother to fire up your Lazarus.

I hope you'll understand...

I hope you'll understand, if this time I haven't supplied quite as much help as I often do. I have also allowed software with rough edges out the door. I hoe you will agree that most of the things on this site are more polished. But if I polish and explain this further, it will be a while that it, however imperfect, is available to you.

   Search this site or the web        powered by FreeFind
  Site search Web search
Site Map    What's New    Search   BEWARE: There is stuff at my other two sites that the search above won't reveal. Go to either site (see links below) and use that site's FreeFind search button.

BEWARE: The search above only visits a selection of my stuff. Go to either of my other sites (see links below) and use that site's FreeFind search button, if you haven't found something you "know is there".

In addition to the tutorials for which this page serves as Table of Contents, I have other sites with material you might find useful.....

My other sites....
Sheepdog Software homepage.
My Arunet homepage.

... and some links to specific pages within them you might want....
You can't "play" all day... learn to use the Libre Office/ Open Office database. Free. Multi-platform.
The Arduino- LOTS of fun, for not much money. And beginner (intelligent beginner) friendly. And good pursuit for kids. Combine programming and electronics!
Designing printed circuit boards the KiCad way. Free. Multi-platform. Long established. PCB-fab houses take native KiCad files.
And lastly... Making maps... how we did it before GPS Indulge me? This discusses a worthwhile, fun (if presented intelligently) activity for kids, which can be undertaken on many levels... a simple 20 minutes, or weeks of engaging activity. (Also known to divert susceptible adults.)

Ad from page's editor: Yes.. I do enjoy compiling these things for you... hope they are helpful. However.. this doesn't pay my bills!!! If you find this stuff useful, please at least help me with Facebook links, etc (buttons at top of page). If you run an MS-DOS or Windows PC, please visit my freeware and shareware page, try something? Links on your page to this page would also be appreciated, of course!
Click here to visit editor's freeware, shareware page.

   Here is the way to contact the author of these Lazarus/Delphi tutorials, Tom Boyd.

Click to check for W3.org HTML validity tester Page has been tested for compliance with INDUSTRY (not MS-only) standards, using the free, publicly accessible validator at validator.w3.org. Mostly passes.

AND passes... Click to check CSS validity

One last bit of advice: Be sure you know all you need to about spyware.

. . . . . P a g e . . . E n d s . . . . .