Re: a unicode conversion issue in a file opening command

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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
.



Relevant Pages

  • Re: wchar_t & clistbox
    ... This means you erroneously used char * somewhere in your program. ... functions are just tcs in place of str, but others have _tcs instead of tcs. ... Note that you must define both UNICODE and _UNICODE preprocessor symbols. ... MVP Tips: http://www.flounder.com/mvp_tips.htm ...
    (microsoft.public.vc.mfc)
  • Re: converting const wchar_t * to const char * ?
    ... Thanks for ur reply well I was converting a library source code from ... ANSCI to UNICODE ... InjectMachine(const char * Name,const char * Type) ... MVP Tips:http://www.flounder.com/mvp_tips.htm ...
    (microsoft.public.vc.mfc)
  • Re: Forcing String Literal as Wide Character Unicode
    ... string literal to either a char or a wchar depending on what is ... but I would like to force my string literal to be a Unicode ... Visual C++ MVP ...
    (microsoft.public.vc.mfc)
  • Re: CString to const char*
    ... I don't have that class available for some reason, ... I only have CString which relies on the UNICODE switch to determine what ... But CStringA only casts to const char * and CStringW only casts to const ...
    (microsoft.public.vc.language)
  • Re: How to LPCTSTR Convert to char *
    ... number of people who use 'char' because they've never grown beyond their first programming ... These are the people who are getting nuked by VS2005 which defaults to Unicode apps. ... isolated to the embedded interface (rare and exotic situation imposed by external ... fields with char strings is quite essential. ...
    (microsoft.public.vc.mfc)