please HELP! Trouble with unicode - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

please HELP! Trouble with unicode

I'm using CreateFile/ReadFile/WriteFile for reading and writing to text files. I have the text files pre-saved as Unicode, but when I open them with CreateFile the text files then saves as an ANSI file. All my code uses Unicode strings and function calls. This is real embarrassing. I used to consider myself a decent Windows Developer but I've been hitting nothing but brick walls with Unicode. I know there has to be a way to change the file attribute to Unicode before I close the handle.

NEVERMIND!!! problem fixed. In case you are wondering here is the fixed test program:

#include
#include


INT _tmain(INT argc, TCHAR **argv[])
{
HANDLE hFile = NULL;
DWORD numBytes = 0;
TCHAR szBuffer[] = TEXT("Hello World.");

hFile = CreateFile(TEXT("myfile.txt"),GENERIC_READ | GENERIC_WRITE,FILE_SHARE_READ,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);

if(hFile == INVALID_HANDLE_VALUE)
{
MessageBoxEx(NULL,TEXT("Could not open specified file."),TEXT("Error"),MB_ICONERROR,LANG_ENGLISH);
return 1;
}

WriteFile(hFile,(LPCVOID)szBuffer,sizeof(TCHAR)*_countof(szBuffer),&numBytes,NULL);

CloseHandle(hFile);
hFile = NULL;
return 0;
}

/* i'm still wondering if the sizeof(TCHAR)*_countof(szBuffer) is an error though. Should I use lstrlen instead of _countof?
Sign In or Register to comment.