Serializing to / from a text file | CodeGuru

Serializing to / from a text file

To serialize the tree view control override the Serialize() function. The Serialize() function is a virtual function defined in Cobject. In the code below we save the outline to a text file and can read it back from a text file. When saving the outline to the archive, tabs are used to indent the item […]

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




To serialize the tree view control override the Serialize() function. The


Serialize() function is a virtual function defined in Cobject.

In the code below we save the outline to a text file and can read it
back from a text file. When saving the outline to the archive, tabs are
used to indent the item text. Again, when reading back, tabs are used to
determine the level that the newly read item should be placed at.

 

void CTreeCtrlX::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		// storing code
		HTREEITEM hti = GetRootItem();
		while( hti )
		{
			int indent = GetIndentLevel( hti );
			while( indent– )
				ar.WriteString( “t” );
			ar.WriteString( GetItemText( hti ) + “rn”);
			hti = GetNextItem( hti );
		}
	}
	else
	{
		// loading code
		CString sLine;
		if( !ar.ReadString( sLine ) )
			return;
		HTREEITEM hti = NULL;
		int indent, baseindent = 0;
		while( sLine[baseindent] == ‘t’ )
			baseindent++;
		do
		{
			if( sLine.GetLength() == 0 )
				continue;
			for( indent = 0; sLine[indent] == ‘t’; indent++ )
				;		// We don’t need a body
			sLine = sLine.Right( sLine.GetLength() – indent );
			indent -= baseindent;
			HTREEITEM parent;
			int previndent = GetIndentLevel( hti );
			if( indent ==  previndent)
				parent = GetParentItem( hti );
			else if( indent > previndent )
				parent = hti;
			else
			{
				int nLevelsUp = previndent – indent;
				parent = GetParentItem( hti );
				while( nLevelsUp– )
					parent = GetParentItem( parent );
			}
			hti = InsertItem( sLine, parent ? parent : TVI_ROOT, TVI_LAST );
		}while( ar.ReadString( sLine ) );
	}
}
int CTreeCtrlX::GetIndentLevel( HTREEITEM hItem )
{
	int iIndent = 0;
	while( (hItem = GetParentItem( hItem )) != NULL )
		iIndent++;
	return iIndent;
}
// GetNextItem	– Get next item as if outline was completely expanded
// Returns		– The item immediately below the reference item
// hItem		– The reference item
HTREEITEM CTreeCtrlX::GetNextItem( HTREEITEM hItem )
{
	HTREEITEM	hti;
	if( ItemHasChildren( hItem ) )
		return GetChildItem( hItem );		// return first child
	else{
		// return next sibling item
		// Go up the tree to find a parent’s sibling if needed.
		while( (hti = GetNextSiblingItem( hItem )) == NULL ){
			if( (hItem = GetParentItem( hItem ) ) == NULL )
				return NULL;
		}
	}
	return hti;
}


 
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.