Results 1 to 8 of 8

Thread: [RESOLVED] RTF file reader

  1. #1

    Thread Starter
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    Resolved [RESOLVED] RTF file reader

    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.
    “victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha

  2. #2
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: RTF file reader

    What is the error? Its no doubt some sort of less-descriptive-than-a-linker-error MFC Error.

    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

  3. #3

    Thread Starter
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    Re: RTF file reader

    Here it is,

    Code:
    unresolved external symbol "class ATL::CStringT > > __cdecl getText(class ATL::CStringT > >;)" (?getText@@YA?AV?$CStringT@DV?$StrTraitMFC_DLL@DV?$ChTraitsCRT@D@ATL@@@@@ATL@@V12@@Z) referenced in function _main
    Actually I found the error, just passing the CString to the getText() it works, like getText(const CString rtf).

    But why is that.

    And also I have another question. I want to find the '\pard' of the rtf CString and replace it with a space. I try this inside the for loop,

    rtf.Replace(_T("\\pard"), _T(" "));

    But it wont work. Any idea about that...
    “victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha

  4. #4
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: RTF file reader

    Okay..

    Is getText() declared? Are they in the same files? Are the files included for the getText() function?

    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

  5. #5

    Thread Starter
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    Re: RTF file reader

    Quote Originally Posted by chemicalNova
    Is getText() declared?
    Yes, it is declared as follows.

    Code:
    CString getText(CString);
    Quote Originally Posted by chemicalNova
    Are they in the same files?
    You mean both main function and the getText? Yes, both are in same file.

    Quote Originally Posted by chemicalNova
    Are the files included for the getText() function?

    You mean header files? Use all the required header files.
    “victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha

  6. #6

    Thread Starter
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    Re: RTF file reader

    Here is my full code.
    Attached Files Attached Files
    “victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha

  7. #7
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: RTF file reader

    Looks to me like you're passing a char pointer into the getText function.. when it expects it to be a CString type..

    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

  8. #8

    Thread Starter
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    Re: RTF file reader

    Yes that is, So I change the way I do this. Used RichEditControl. It's so easy, just two line of code need to do it.
    “victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width