A class for double zeroterminated strings | CodeGuru

A class for double zeroterminated strings

Download Source Code and Example There are several win32 functions that require the use of a string with embedded NULLs and are terminated with an extra NULL (i will call them DZ strings for short). An example of such a DZ string: “part1part2part3”. To name just a few of functions that work with DZ strings: […]

Written By
CodeGuru Staff
CodeGuru Staff
Aug 22, 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

Download Source Code and Example

There are several win32 functions that require the use of a string with embedded
NULLs and are terminated with an extra NULL (i will call them DZ strings for short).
An example of such a DZ string: “part1part2part3”.
To name just a few of functions that work with DZ strings:

  • GetPrivateProfileSection
  • WritePrivateProfileSection
  • SQLConfigDataSource

As most string-functions (strcmp, strchr etc) expect a zeroterminated string, they are of little use when
you have to parse a DZ string.
The CString class could be used to manipulate them, but you must be carefull not to call the non-DZ-aware
CString-methods (such as Right(), Mid(), Left(), Replace() and Find()).
I decided to create a class that helps constructing and parsing those DZ strings.
The class is called CDoubleZeroString and is not derived from CString.

An example how to use this class:

	CDoubleZeroString dz;
	dz.Add(“a=1”); // add a zeroterminated ‘part’
	dz.Add(“b=2”);
	dz.Add(“c=3”);
		// operator(const char*)  will return “a=1b=2c=3”
	WritePrivateProfileSection(“humbug”, dz, “.\humbug.ini”);
	char	buffer[1024];
	GetPrivateProfileSection(“humbug”, buffer, sizeof buffer, “.\humbug.ini”);
		// cleanup dz
	dz.Empty();
		// AddDoubleZero will parse buffer
	dz.AddDoubleZero(buffer);
		// print the outcome
	for(int i=0;i<dz.GetCount();i++)
		cout << dz.Get(i) << endl;
	CString test = dz;
	cout << “cstring ” << test << endl;

This will show the following output:
a=1
b=2
c=3
cstring a=1

The CString test will only copy the first substring when it is constructed…

If you want to implement this using CString you would have to do something like:

	CString	dz(“a=1” “b=2” “c=3” “”, 13);
	   // or some prefer:
	CString dz(“a=1@b=2@c=3@@”);
	int pos;
	   // note that you MUST use ReverseFind (not Find) for this to work
	while((pos=dz.ReverseFind(“@”))!=-1)
		dz.SetAt(pos, ‘’);

Last updated: August 17, 1998

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.