How can read a unicode text file as character by character?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: How can read a unicode text file as character by character?

  1. #1
    Join Date
    Jan 2008
    Location
    India
    Posts
    780

    How can read a unicode text file as character by character?

    Hi all,

    i have and unicode file, i wanna read this file by character wise,means read only one character at a time.

    please tell me how can i do this.

    thanks in advace.
    IN A DAY, WHEN YOU DON'T COME ACROSS ANY PROBLEMS - YOU CAN BE SURE THAT YOU ARE TRAVELLING IN A WRONG PATH

  2. #2
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: How can read a unicode text file as character by character?

    Compile your application in UNICODE and read the file as you normally do.

  3. #3
    Join Date
    Jan 2008
    Location
    India
    Posts
    780

    Re: How can read a unicode text file as character by character?

    i compile the application with unicode character set and using this code but its not working fine and read some garbage value.

    Code:
    	CStdioFileEx SntItm;
    	CString Sent, StrFile;
    
    	Sent=_T("E:\\a.txt");
    	
    	if(FAILED(SntItm.Open(Sent,CFile::modeRead,NULL)))
    		return;
    	ULONGLONG i=0;
    	ULONGLONG flen=SntItm.GetLength();
    	LPCTSTR ch[10];
    	
    	while(i<flen)
    	{			
    		SntItm.Read(ch,1);
    		if(ch[0]==0)
    		{
    			i++;
    			continue;
    		}
    		StrFile+=ch[0];
    		i++;
    	}
    	SntItm.Close();
    IN A DAY, WHEN YOU DON'T COME ACROSS ANY PROBLEMS - YOU CAN BE SURE THAT YOU ARE TRAVELLING IN A WRONG PATH

  4. #4
    Join Date
    Jul 2009
    Location
    India
    Posts
    831

    Re: How can read a unicode text file as character by character?

    Note: You are using CStdioFileEx ie: a custom class derived from MFC Class CStdioFile with some modification. So be sure that you are using the latest version and provide proper implementation of the header and source of the library.
    ◄◄ hypheni ►►

  5. #5
    Join Date
    Jul 2009
    Location
    India
    Posts
    831

    Re: How can read a unicode text file as character by character?

    And did you give a try with wifstream instead of mfc class ?

    And lets have a look on this stackoverflow topic.
    Last edited by hypheni; March 21st, 2011 at 08:13 AM.
    ◄◄ hypheni ►►

  6. #6
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,560

    Re: How can read a unicode text file as character by character?

    Code:
    LPCTSTR ch[10];
    .
    why LPCTSTR ? I thiank it should be TCHAR instead

    Also, do you want binary or text mode ?

  7. #7
    Join Date
    Jan 2008
    Location
    India
    Posts
    780

    Re: How can read a unicode text file as character by character?

    Now i m using the CStdioFile file but the same results ,its unable to read unicode file.

    Code:
    CStdioFile SntItm;
    	CString Sent, StrFile;
    
    	Sent=_T("E:\\a.txt");
    	
    
    	if(FAILED(SntItm.Open(Sent,CFile::modeRead | CFile::typeText | CFile::typeBinary  ,NULL)))
    		return;
    	ULONGLONG i=0;
    	ULONGLONG flen=SntItm.GetLength();
    //	LPCTSTR ch[10];
    	TCHAR ch[10];
    	
    	while(i<flen)
    	{
    		SntItm.Read(ch,1);
    		if(ch[0]==0)
    		{
    			i++;
    			continue;
    		}
    		StrFile+=ch[0];
    		i++;
    	}
    	SntItm.Close();
    	AfxMessageBox(StrFile);
    please help me for this.
    IN A DAY, WHEN YOU DON'T COME ACROSS ANY PROBLEMS - YOU CAN BE SURE THAT YOU ARE TRAVELLING IN A WRONG PATH

  8. #8
    Join Date
    Jul 2009
    Location
    India
    Posts
    831

    Re: How can read a unicode text file as character by character?

    See IO stream does not care about file encoding and just reads raw bytes from file. I'm not sure but most probably the same is applied for MFC Class. You should go through the thread of Stackoverflow that I have posted earlier.
    ◄◄ hypheni ►►

  9. #9
    Join Date
    May 2002
    Posts
    1,435

    Re: How can read a unicode text file as character by character?

    Just a few of the errors with your code:

    (1) CFile::typeText | CFile::typeBinary - Conflicting types, besides being irrelevant for CStdioFile which always uses CFile::typeText regrardless of what you specifiy.

    (2) FAILED() is intended for use with HRESULT, not BOOL.

    (3) LPCTSTR ch[10] or TCHAR ch[10] should be single wchar_t for UNICODE.

    (4) Use of 'flen'. Length of file in bytes and related indexing can not be used when reading UNICODE characters.

    (5) BOM (byte order mark) is ignored. Assumes file is UNICODE. Should really check before proceeding.

    (6) There are subtle issues with using CStdioFile for UNICODE files so it may be better to us a file object that forces you to work with raw bytes.

    Code:
    	CStdioFile SntItm;
    	CString Sent = _T("E:\\a.txt");
    	CString StrFile;
    
    	if(SntItm.Open(Sent, CFile::modeRead | CFile::typeText, NULL))
    	{
    		wchar_t ch;
    	
    		while(sizeof(wchar_t)==SntItm.Read(&ch, sizeof(wchar_t)))
    		{
    			StrFile += ch;
    		}
    	
    		SntItm.Close();
    
    		AfxMessageBox(StrFile);
    	}
    The corrected code that I posted is not really correct in the sense that it sloppily mixes CString, TCHAR and wchar_t assuming that they are all the same thing. This will ONLY work for a UNICODE build which is NOT the proper way to deal with UNICODE files in MFC. However, since you probably want something simple rather than something correct, I post the simple method.
    Last edited by 0xC0000005; March 22nd, 2011 at 06:43 AM.

  10. #10
    Join Date
    Jan 2008
    Location
    India
    Posts
    780

    Re: How can read a unicode text file as character by character?

    thanks.its done with your help.
    IN A DAY, WHEN YOU DON'T COME ACROSS ANY PROBLEMS - YOU CAN BE SURE THAT YOU ARE TRAVELLING IN A WRONG PATH

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center