Mapped File Class | CodeGuru

Mapped File Class

–> CMappedFile was created after some users complained that my apps were storing too much data in the Registry. I needed a way to preserve the multilevel data heirarchy that the Registry provides, but I needed to do it in external files. In addition, I wanted some protection from curious eyes. CMappedFile is essentially a […]

Written By
CodeGuru Staff
CodeGuru Staff
Nov 13, 1998
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

–>

CMappedFile was created after some users complained that my apps were
storing too much data in the Registry. I needed a way to preserve the
multilevel data heirarchy that the Registry provides, but I needed to
do it in external files. In addition, I wanted some protection from
curious eyes.

CMappedFile is essentially a CMap of COleVariants that will serialize
itself to and from files. It supports long, bool,
LPCSTR
and double. But, because you
can cast just about any basic type into a long,
you can store just about anything. Plus, it should be fairly simple to
extend this class to hold anything a COleVariant can hold.

To provide key support, three functions, SetBaseKey,
SetSubKey and MoveUp
are provided.

  • SetBaseKey(key) allows you to set
    the root key for all
    subsequent read and write operations.
  • SetSubKey(subKey) moves the current key
    “down” to the specified subkey.
  • MoveUp() moves the current key “up” to the
    parent key.

The following example write five values to a CMappedFile, in two different
keys, and writes the file. Then, it reads the file back into a different
CMappedFile and verifies the results. Pretty simple.

// stupid data values
double pi = 3.14159;
CString hi = "Hi There!";
long fiftyFive = 55;
bool thisWorks = true;
bool thisDoesNotWork = false;

CMappedFile mf;

// start off with a key called "Root"
mf.SetBaseKey("Root");

// Root\Pi : 3.14159
mf.Set("Pi", pi);

// Root\Hi : Hi There!
mf.Set("Hi", hi);

// move down a subkey
mf.SetSubKey("Subkey");

// Root\Subkey\Success : true
mf.Set("Success", thisWorks);

// Root\Subkey\55 : 55
mf.Set("55", fiftyFive);

mf.MoveUp();
// Root\Failure : false
mf.Set("Failure", thisDoesNotWork);

// dump this to the debug output window
mf.Dump();

// save it to a file
mf.Write("test.mf");

// "test.mf" now holds the following values : 
// Root\Pi : 3.14159
// Root\Hi : Hi There!
// Root\Subkey\55 : 55
// Root\Subkey\Success : true
// Root\Failure : false

// read it back, to a different CMappedFile, to be safe
CMappedFile mf2;
mf2.Read("test.mf");

mf2.SetBaseKey("Root");
ASSERT(mf2.Get("Hi", "Bye") == hi);
ASSERT(mf2.Get("Pi", -1.0) == pi);

mf2.SetSubKey("Subkey");
ASSERT(mf2.Get("55", (long)-1) == fiftyFive);
ASSERT(mf2.Get("Success", false) == true);

mf2.MoveUp();
ASSERT(mf2.Get("Failure", true) == false);

Download source – 4 KB

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.