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.
Printable View
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.
Compile your application in UNICODE and read the file as you normally do.
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();
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.
And did you give a try with wifstream instead of mfc class ?
And lets have a look on this stackoverflow topic.
.Code:LPCTSTR ch[10];
why LPCTSTR ? I thiank it should be TCHAR instead
Also, do you want binary or text mode ?
Now i m using the CStdioFile file but the same results ,its unable to read unicode file.
please help me for this.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);
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.
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.
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.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);
}
thanks.its done with your help.