Safe File Operations

Publication: C++ Builder Developer's Journal
Issue: Volume 7, Number 4, April 2003

In this article I discuss 2 major functions, LoadFromFile and SaveToFile and the coding techniques you can utilize to minimize problems. For instance LoadFromFile does not work very well if you're trying to load a file that has been corrupted (i.e. a simple text file that has been renamed with a .png extension). SaveToFile can also through you a curve ball if the file you are attempting to save to is read-only.

There are a number of VCL components that support a LoadFromFile() and SaveToFile() method. In most circumstances these methods behave as expected, however if unexpected behaviour occurs (and it can), then this can be the Achilles heal in an application if programmers do not take the appropriate precautions to avert unwanted behaviour. This article will discuss some of the problems associated with the LoadFromFile() and SaveToFile() methods, and offer suggestions to avoid unwanted and unhandled exceptions.