Hi all,

I want to read a RTF file and get the text only from it. So I tried the following code.

Main function.

Code:
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
		_tprintf(_T("Fatal Error: MFC initialization failed\n"));
		nRetCode = 1;
	}
	else
	{
		// TODO: code your application's behavior here.
		CFile rtfFile;// RTF file object
		bool err = rtfFile.Open("G:\\Work On\\CPP\\Counter\\TestFile.rtf", CFile::modeReadWrite, NULL);

		if(err != 0)
		{
			// Use the file size buffer
			int length = rtfFile.GetLength();
			char *pbuff = new char[length];

			// Read the file to buffer
			rtfFile.Read(pbuff, length);

			CString text(getText(pbuff));

			//cout << (LPCTSTR)text;
		}
	}
	return nRetCode;
}
getText function.

Code:
CString getText(const CString & rtf)
{
	CString strCopy;// Hold the text
	CString ch;// For next character
	CString str(rtf.GetAt(1));

	BOOL bBrace = FALSE;// for '{'
	BOOL bSlash = FALSE;// for '\'
	BOOL bFirstLetter = FALSE;// first letter after the '\' sign

	int nLength = rtf.GetLength();// Get the length again

	// Just use as the dummy conditions. Length should be more that one
	if (nLength < 4)
	{
		return "";
	}

	// Loop the length until find the EOF, using length of the RTF file
	for (int i = 0; i < nLength; i++)
	{
		ch = rtf.GetAt(i);

		if (ch.Find('\\') != -1)
		{
			bSlash = TRUE;
			continue;
		}
		else if (ch.Find(' ') != -1)
		{
			bSlash = FALSE;
			//Let it fall through so the space is added
			//if we have found first letter
			if (!bFirstLetter)
			{
				continue;
			}
		}
		else if (ch.Find('{') != -1)
		{
			bBrace = TRUE;
			bSlash = FALSE;
			continue;
		}
		else if (ch.Find('}') != -1)
		{
			bSlash = FALSE;
			bBrace = FALSE;
			continue;
		}

		if (!bSlash && !bBrace)// check both '\' and '{' are avoided
		{
			if (!bFirstLetter)// set the first letter
			{
				bFirstLetter = TRUE;
			}
			strCopy += ch;
			continue;
		}
	}
	return strCopy;
}
But I got an error on main function, when I call the getText function to find the text. I can't find any solution for that. Can you guys give a clue to me.