Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi! I have unicode MFC application.

char strSendFilePath[MAX_FILE_PATH] = {ZERO_VAL};
wcstombs(strSendFilePath,   strSendTextPath.GetBuffer(strSendTextPath.GetLength()), strSendTextPath.GetLength());
strSendTextPath.ReleaseBuffer();

FILE *fpRead;
const int l_size = CHUNK_SIZE;
char file_buffer[l_size];
if((fpRead = fopen(strSendFilePath, "rb")) == NULL)
{
    AfxMessageBox(IDS_FILE_NOTOPEN);
    return FALSE;
}


It is working fine, if the folder path or file path like this: C:\Documents and Settings\user\My Documents\Downloads\Sample.txt

If suppose folder path in japanese,
C:\Documents and Settings\user\My Documents\Downloads\おはようございます\Sample.txt

then fopen is failed.

please do the needful.


Thanks much
Sam.
Posted
Updated 11-Feb-13 4:04am
v2
Comments
Matthew Faithfull 11-Feb-13 10:08am    
I seem to remember an MSDN page with almost exactly this issue, perhaps you can find it. Remember fopen is a C library functions that takes an ordincary C style string of 8 bit characters. You japanese path name is coming from what is essentially a UTF8 file system via a UNICODE application and being passed to an ANSI C function. Somewhere along the way a Code Page is wrong or a conversion is missing. Seek and yee shall find and all that.

Basically, you need to use all the functions associated with Wide-characters in C. It's been a resurgent topic to me, to be able to output into stream and files Japanese characters.
In this case, I have myself tested that it actually works.
If you don't know exactly what you are doing, ask me, since I can provide you with the necessary material for the task.

First, you'll declare a variable
C++
wchar_t jpSentence[100] = L"今はVisualC++2010を試みていますよ。";

It is compulsory to say that "TCHAR" and "wchar_t" are the same types only if "_UNICODE" macro has been set and your projects files are in saved in UNICODE.

Once done this you have to open the file:

C++
FILE *fp = _wfopen(L"japfile.txt", L"w, ccs=UTF-8");

We create a pointer to a FILE. Then we open it with "_wfopen" (which is the correspondent function to open wide character fies) and we use the arguments
"japfile.txt" (in my case is the filename I wish to write into).

Then on the second argument I specify two things the mode:
The first one, is that I want to write, "w".
The second one, is that I want to specify the encoding (ccs) of file the file to be saved as "UTF-8".
If you do omit this last step, when you open your notepad you'll see "?" on positions
where Japanese characters are located. So it is very important. Since the file will be written as ANSI.

Note: Just for you to know (If you didn't know) the "L" stuck right at the left of wide-character strings has to be put in order to make the compiler you are completely conscious that you are using a wide-character string. If you wish to compile your project as both UNICODE and ANSI (that is making two different executable versions) use the TEXT("") macro. Depending if the _UNICODE macro has been defined or not, all TCHAR will be either "char" or "wchar_t" and your strings will be prefixed with the "L" automatically when the TEXT() macro encountered.

Finally,

C++
fwprintf(fp,L"%ls",jpSentence);
fclose(fp);


You can see that "fwprintf" stands for "File Wide PRINT Function".
The second argument has to be specified as "%ls", meaning "long string".

Finally that's all for now.
I really encourage you to take a deeper look into these webpage references to understand more how it works and what other functions you can use:
http://www.cplusplus.com/reference/cwchar/[^]

http://publib.boulder.ibm.com/infocenter/iseries/v7r1m0/index.jsp?topic=%2Frtref%2Ffwprintf.htm[^]

and

http://msdn.microsoft.com/es-es/library/yeby3zcb%28v=vs.80%29.aspx[^]

P.S: For your directory problem, you should do something like:
C++
FILE *fp = _wfopen(L"幹事の方/japfile.txt", L"w, ccs=UTF-8");

FILE *fp = _wfopen(L"幹事の方/japfile.txt", L"w, ccs=UTF-8");

The final code should look like this:

C++
#include <Windows.h>
#include <wchar.h> // Include only if you are not using Windows.h
#include <stdio.h>
//The directory has 幹事の方 to exist in order not to make happen any error
FILE *fp = _wfopen(L"幹事の方/japfile.txt", L"w, ccs=UTF-8");
	if(!fp)
	{
		exit(EXIT_FAILURE);
	}
	wchar_t jpSentence[100] = L"今はVisualC++2010を試みていますよ。";
	fwprintf(fp,L"%ls",jpSentence);
fclose(fp);
exit(EXIT_SUCCESS);
 
Share this answer
 
v4
Comments
Mr Sam 11-Feb-13 20:21pm    
Thanks you!!!
unscathed18 12-Feb-13 15:15pm    
excuse me, would you mind giving me some star points if it's been any helpful to you? I don't intend to be harsh :)
I see you mixing unicode with non-unicode calls and code, you should stick to unicode calls only and everything will work better. Don't use char at all, either use wide characters or use one of the many MFC macros that allow you to do compilation in any mode.

fopen/wfopen:
http://msdn.microsoft.com/en-us/library/yeby3zcb%28v=vs.80%29.aspx[^]

MFC helper macros:
What are TCHAR, WCHAR, LPSTR, LPWSTR, LPCTSTR (etc.)?[^]
 
Share this answer
 
Comments
SajeeshCheviry 11-Feb-13 12:18pm    
hm...good answer
Albert Holguin 11-Feb-13 12:51pm    
thanks
Mr Sam 11-Feb-13 20:21pm    
Albert, thanks to you!!
Albert Holguin 11-Feb-13 20:29pm    
sure, good luck n keep coding! :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900