Click here to Skip to main content
15,892,161 members
Articles / Desktop Programming / MFC

A UTF-16 Class for Reading and Writing Unicode Files

,
Rate me:
Please Sign up or sign in to vote.
4.86/5 (18 votes)
15 Jul 2009CPOL2 min read 549.6K   1.6K   56  
A UTF-16 class derived from CStdioFile for reading and writing Unicode files
��// utf16test.cpp : Defines the entry point for the console application.

//



#include "stdafx.h"



#include "UTF16File.h"



#ifdef _DEBUG

#define new DEBUG_NEW

#undef THIS_FILE

static char THIS_FILE[] = __FILE__;

#endif



/////////////////////////////////////////////////////////////////////////////

// The one and only application object



CWinApp theApp;



// Removed by JW (sloppy)

// using namespace std;



int _tmain(int /*argc*/, TCHAR* /*argv*/[], TCHAR* /*envp*/[])

{

	int nRetCode = 0;

	

	// initialize MFC and print and error on failure

	if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))

	{

		// TODO: change error code to suit your needs

		std::cerr << _T("Fatal Error: MFC initialization failed") << std::endl;

		nRetCode = 1;

	}

	else

	{

		

        TRY

		{

            ///////////////////////////////////////////////

            ///////////////////////////////////////////////

            //

            // Write Tests

            //

            ///////////////////////////////////////////////

            ///////////////////////////////////////////////

			CString strLen;



			OutputDebugString(_T("Creating test files\n\n"));

			

            CUTF16File output1(_T("unicode_write.txt"), CFile::modeWrite | CFile::modeCreate);

			strLen.Format(_T("Before write, unicode_write.txt: %d bytes\n"), output1.GetLength());OutputDebugString(strLen);

			

			output1.WriteString(_T("SOME_KEY=C@A82\n"), TRUE);

			output1.WriteString(_T("Hello World from Unicode land!"), TRUE);

			strLen.Format(_T("After write,  unicode_write.txt: %d bytes\n\n"), output1.GetLength());OutputDebugString(strLen);

			

            output1.Close();

			

            CUTF16File output2;

			output2.Open(_T("ansi_write.txt"), CFile::modeWrite | CFile::modeCreate);

			strLen.Format(_T("Before write, ansi_write.txt: %d bytes\n"), output2.GetLength());OutputDebugString(strLen);

			

            output2.WriteString(_T("Hello World from ANSI land!"));

			strLen.Format(_T("After write,  ansi_write.txt: %d bytes\n\n"), output2.GetLength());OutputDebugString(strLen);

            

			output2.Close();





            ///////////////////////////////////////////////

            ///////////////////////////////////////////////

            //

            // Read Tests

            //

            ///////////////////////////////////////////////

            ///////////////////////////////////////////////



            CString szInput;



            CUTF16File input1(_T("unicode_write.txt"), CFile::modeRead);        



            input1.ReadString(szInput);



            OutputDebugString(_T("Unicode read test:\n"));

            OutputDebugString(szInput);

            OutputDebugString(_T("\n\n"));



            input1.Close();



            CUTF16File input2;

            

            input2.Open( _T("ansi_write.txt"), CFile::modeRead);        

            input2.ReadString(szInput);



            OutputDebugString(_T("ANSI read test:\n"));

            OutputDebugString(szInput);

            OutputDebugString(_T("\n\n"));



            input2.Close();

        }

		CATCH(CFileException, e)

		{

            #ifdef _DEBUG

                afxDump << _T("File error: ") << e->m_cause << _T("\n");

            #endif

				e->Delete();

        }

		END_CATCH

	}



	return nRetCode;

}





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 Code Project Open License (CPOL)


Written By
Systems / Hardware Administrator
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Written By
Software Developer (Senior)
United Kingdom United Kingdom
Ok, it's about time I updated this profile. I still live near 'Beastly' Eastleigh in Hampshire, England. However I have recently been granted a permamant migration visa to Australia - so if you're a potential employer from down under and like the look of me, please get in touch.
Still married - just, still with just a son and daughter. But they are now 8 and 7 resp and when together they have the energy of a nuclear bomb.
I worked at Teleca UK for over 8.5 years (but have now moved to TikitTFB) and have done loads of different things. Heavily involved with MFC, SQL, C#, The latest is ASP.NET with C# and Javascript. Moving away from Trolltech Qt3 and 4.
Jordan.

Comments and Discussions