News | Articles | Libraries | Developer Tools | Books | Forum Links | Search   
Sections:
 

QA: How do I read from and write to an ini file?

By Joao Paulo Figueira, February 9, 2004.
Print version

Question

I want to read from a ini file and write back any changes. Is there an API for this?

Answer

Ini files are not supported in Windows CE. Microsoft advises us to use the registry as a means to store persistent application settings and other information. Accessing the registry is thoroughly documented on this site on (Files and Registry).

There are however good reasons why you would want to use ini files on your application. One of the reasons that immediately springs to mind is the support for legacy applications. Another equally valid reason is the apparent ease with which you can access the ini information. An ini file is a regular text file that you can edit using whatever editor you choose.

Finally, you may find it easier to update your customer's ini files than their device's registry entries. Updating an ini file is just a simple matter of replacing it, whereas registry entries require some programming to change (there is no standard registry editor on the Pocket PC).

This article describes a simple solution for the problem of reading and writing ini files from whithin a C++ application. This solution is illustrated by a sample application (see below - project files for eVC3).

The CIniFile Class

Ini files are managed by objects of the CIniFile class (see sample code). Information is serialized on a standard Unicode text file (Unicode header is required) and has the following general format:

[Section] Entry=Value

Sections are unique on an ini file and entries are unique in a section. Both section and entry names are case sensitive. The value can be any string up to the end of line. There is no limit on the size of strings. No comments are supported on this version.

Objects are created through the constructor CIniFile::CIniFile(LPCTSTR pszFile = NULL). If you supply a file name onthe constructor, the file will be loaded immediately. Otherwise, you can load it by calling BOOL CIniFile::Load(LPCTSTR pszFile = NULL). When you are done using the ini file, you can call BOOL CIniFile::Save(LPCTSTR pszFile = NULL) filling in the file name if the file was not loaded or created empty. If the file was loaded or created with a name, the destructor will save it for you.

Setting and retrieving values is achieved through the Get and Set family methods. These have a consistent signature:

BOOL Get(LPCTSTR pszSection, LPCTSTR pszEntry, CString &strValue); BOOL Set(LPCTSTR pszSection, LPCTSTR pszEntry, const CString &strValue);

You have to provide the section name, entry name and the value as the last argument. These methods can be extended to support any type you require. In this implementation, I provide two other types just to illustrate the implementation.

BOOL Get(LPCTSTR pszSection, LPCTSTR pszEntry, int &nVal); BOOL Set(LPCTSTR pszSection, LPCTSTR pszEntry, int nVal); BOOL Set(LPCTSTR pszSection, LPCTSTR pszEntry, COLORREF rgb); BOOL Get(LPCTSTR pszSection, LPCTSTR pszEntry, COLORREF &rgb);

Please study their implementations before implementing your own types.

All methods return a BOOL value that reflects the success status of the operation. The Set methods will create both section and entry if these do not exist. Once again, remember about case sensitivity!

Implementation Details

There are a number of support classes that are required in order to make CIniFile work. All of them are included on the sample code, except the STL library. The one I use in all my projects is STL for Windows CE.

IniEdit

The sample application is an ini file editor for the Pocket PC. It will allow you to create and modify ini files. You can download it here - IniEdit.zip (114 Kb).

And that's it.

Related resources:

Discuss

Discuss this article. Here you can write your comments and read comments of other developers.
Rate this article:     Poor Excellent    
 12345 
© 2001-2005 Pocket PC Developer Network, a division of Spb Software House