Re: a unicode conversion issue in a file opening command
- From: Joseph M. Newcomer <newcomer@xxxxxxxxxxxx>
- Date: Thu, 10 Sep 2009 22:20:32 -0400
Several issues.
On Thu, 10 Sep 2009 15:31:39 -0700, JD <jdt_young@xxxxxxxxx> wrote:
Hi,****
I have a piece of code below that has worked for years. Recently, it
failed to open a file with Japanese characters. Can somebody help me to
identify the error? _UNICODE is defined in my application. Thanks for
any hint or help. JD
Did you define the symbol UNICODE also? If not, the results are undefined.
****
****
USES_CONVERSION;
CString sFile;
....
char *sFileName = T2A((LPTSTR)(LPCTSTR)sFile);
Why use char*? What's wrong with sFile?
****
int fh = _open(sFileName, _O_RDONLY|_O_BINARY);****
You should not be using _open for anything; this is so quaintly 1975 PDP-11 code I didn't
know anyone actually *used* it in modern programming. What's wrong with CFile? Or
CStdioFile?
Apparently, the only reason you need to create a char* pointer is because you are using an
obsolete call. If you use _topen, it will accept a Unicode name in a Unicode build and an
ANSI name in an ANSI build. Or use _wopen. Or use something contemporary, like CFile.
****
if (fh == -1)****
{
AfxMessageBox(_T("File cannot be opened."));
return (1);
I hope this AfxMessageBox call is meant as an illustration, rather than representing real
code. In a real AfxMessageBox, you would display the file name, and the error code that
explained why it failed, and better still, the text string that represents the reason it
failed. As shown, this is an example of worst-methodology programming.
Note that return is not a function call, and therefore there is no reason to enclose the
values returned in parentheses.
****
}****
......
// the file reading code from here is very long so that I don't want to
change it to use other binary reading routines
Then use _topen.
joe
****
Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.
- Follow-Ups:
- Re: a unicode conversion issue in a file opening command
- From: Mihai N.
- Re: a unicode conversion issue in a file opening command
- References:
- Prev by Date: Re: List of Code Pages API
- Next by Date: Re: FreeImage
- Previous by thread: Re: a unicode conversion issue in a file opening command
- Next by thread: Re: a unicode conversion issue in a file opening command
- Index(es):
Relevant Pages
|