Click here to Skip to main content
15,860,861 members
Articles / Programming Languages / Visual C++ 10.0

Linq-To-XML Style of Node Creation for C++

Rate me:
Please Sign up or sign in to vote.
4.78/5 (14 votes)
12 Apr 2016Ms-PL11 min read 43.3K   500   29  
Linq-To-XML Node Creation for Native C++
Latest version have UTF-8 support! Internally it is still stored as unicode.


Notes for using Unicode file class
===================================
The buffer length required by some functions is in terms of wide-characters, not bytes
Example, for writing string of L"Hello", you specify 5, not 10.

==========================
If you specify zero as the buffer length for write functions, it will treat the string to be
written as null terminated.

For Read() function, you must specify the buffer length, except for ReadLine()
You must pass a null pointer to Read() and ReadLine().
You must delete the array returned by Read() and ReadLine() yourself.

==========================
Please use binary mode, when opening unicode file.
Please use text mode, when opening UTF-8 file.

example,

CUnicodeFile uf;
// open for writing
bool bRet = uf.Open( _T("D:\\test.txt"), _T("wb") );

// open for appending
//bool bRet = uf.Open( _T("D:\\test.txt"), _T("ab") );

if( bRet )
{
	uf.WriteLine( _T("Thank god is friday!") );
	...
}

==========================
Call IsUnicodeFile() immediately after opening to determine it is a unicode file
before proceeding.

==========================
Before using ReadLine(), you must call ReadAll() once;

CUnicodeFile uf;
bool bRet = uf.Open( "D:\\uc.txt", "rb" );

if( bRet )
{	
	uf.ReadAll();

	wchar_t *pwchar = 0;
	int nBufLen = 0;

	CString str;
	while( uf.ReadLine( pwchar, nBufLen ) )
	{
		if( pwchar )
		{
			// Do your stuff here			

			delete [] pwchar;
			pwchar = 0;
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
Software Developer (Senior)
Singapore Singapore
Shao Voon is from Singapore. His interest lies primarily in computer graphics, software optimization, concurrency, security, and Agile methodologies.

In recent years, he shifted focus to software safety research. His hobby is writing a free C++ DirectX photo slideshow application which can be viewed here.

Comments and Discussions