Chinese letters

I made a program which opens a .txt file
However when I open some txt file I get some chinese letters and sometimes i get access violation
Any help?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
		case ID_OPEN:{
			OPENFILENAME ofn;
			LPTSTR szFileName = L"";
			
			szFileName = (LPTSTR)GlobalAlloc(GPTR, MAX_PATH);
			ZeroMemory(&ofn, sizeof(OPENFILENAME));
			ofn.hwndOwner		= hwnd;
			ofn.lStructSize		= sizeof (ofn);
			ofn.lpstrFilter		= L"Text(*.txt)\0*.txt\0All Files(*.*)\0*.*\0";
			ofn.lpstrFile		= szFileName;
			ofn.nMaxFile		= MAX_PATH;
			ofn.Flags			= OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
			ofn.lpstrDefExt		= L"txt";
			if(GetOpenFileName(&ofn)){
				HANDLE hFile;

				hFile = CreateFile(szFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
				if(hFile != INVALID_HANDLE_VALUE)
				{
					DWORD dwFileSize;

					dwFileSize = GetFileSize(hFile, NULL);
					if(dwFileSize != -1)
					{
						LPTSTR pszFileText;

						pszFileText = (LPTSTR)GlobalAlloc(GPTR, dwFileSize + 1);
						if(pszFileText != NULL)
						{
							DWORD dwRead;

							if(ReadFile(hFile, pszFileText, dwFileSize, &dwRead, NULL))
							{
								pszFileText[dwFileSize] = '\0';
								SetWindowText(g_edit, pszFileText);
							}
							GlobalFree(pszFileText);
						}
					}
					CloseHandle(hFile);
				}
			}
			}
			break;
I should first say, I don't know why it's crashing, but it would help if you told us where the program is crashing?

BTW, GlobalAlloc/LocalAlloc are hangovers from WIN16. You really should try to avoid them unless you're using an API call that requires one of them. Use HeapAlloc instead, it was introduced in WIN32.

You may find this helpful.
http://msdn.microsoft.com/en-us/library/windows/desktop/aa366533%28v=vs.85%29.aspx
i replaced old global alloc with this:
HeapAlloc(&pszFileText,HEAP_ZERO_MEMORY, dwFileSize +1);
result:
i always get access violation
I didn't suggest that would fix the problem, I was only saying it's the correct thing to do. But you still aren't saying where the program crashes.
now it's always accesss violation on szFileName

b4 it was like this:
when I open the file, it all works well, and then it shows me chinese letters
and if I open another file within few secs I get access violation before open file dialog is shown(if I wait few secs i get again chinese letters)
Try to create the buffer on the stack, like this:
WCHAR szFileName[MAX_PATH + 1] = {0};

Note: Mixing Unicode literal strings and generic windows functions is NOT recommended and most likely error prone.


The error in your code is that GlobalAlloc() only returns a handle to allocated memory, not a real pointer. To get the pointer, use GlobalLock().
http://msdn.microsoft.com/en-us/library/windows/desktop/aa366584(v=vs.85).aspx
Last edited on
if i enter this:
WCHAR szFileName[MAX_PATH + 1] = {0};

and this:
pszFileText = (LPWSTR)GlobalLock(GlobalAlloc(GPTR,dwFileSize +1));

I get an error on compiling saying that pszFileText cannot be matched to that function's return value because pszFileText is not modifiable

If I place this:
WCHAR *pszFileText;

pszFileText = (LPWSTR)GlobalLock(GlobalAlloc(GPTR,dwFileSize +1));

I get again chinese letters and if I quickly try to open another file, bang, access violation
Can you just point to a line in the code you originally presented where the crash occurs. There no point in us scourering your code looking for faults that don't exist when you can just tell us where the crash happens.
Your lpstrFilter parameter must be double nul terminated, which is not in your example. Just copy and paste the damn thing from some website, there are hundreds of examples on the web.
1
2
3
4
5
6
7
8
9
10
11
			
TCHAR szFilters[] = TEXT("Text Files (*.txt)\0*.txt\0\0");
TCHAR szFileName[_MAX_PATH] = TEXT("");
OPENFILENAME ofn = {0};
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = hwnd;
ofn.lpstrFilter = szFilters;
ofn.lpstrFile = szFileName;  // This will the file name
ofn.nMaxFile = _MAX_PATH;
ofn.lpstrTitle = TEXT("Open File");
ofn.Flags = OFN_FILEMUSTEXIST;

i copied this from some other tutorial
yet it doesnt work
for break points:
i haven't seen any break point in code while debugging
this is output form debug:
Windows has triggered a breakpoint in win32 open file.exe.

This may be due to a corruption of the heap, which indicates a bug in win32 open file.exe or any of the DLLs it has loaded.

This may also be due to the user pressing F12 while win32 open file.exe has focus.

The output window may have more diagnostic information.
ok
i found the problem

pszFileText[dwFileSize] = '\0';//i deleted this part of code and now it works
no more access violation is shown

still i got chinese letters (in the .txt file there are no chinese letters only normal latin letters..)

funny thing:
I typed "asdf" in file and I've opened it with my program, google translator has displayed that this is written in chinese:
Xi met Ben B Lane??? Flattering 䬰 ˤ

so..Xi met Ben
Last edited on
You don't know how to use a memory buffer. Please read a decent tutorial and then come back.

Your file is NOT UTF16-LE encoded, as you try to read it by wrongly casting ReadFile() buffer to LPTSTR (which in your build settings means pointer to wchar_t's).
nvm problem solved

solution:
I first stored text from file as ANSI strings then i converted them to UNICODE

anway thx for helping
Topic archived. No new replies allowed.