Print character
-
Hi,
I've already tried to print character such as beta or gamma and it doesn't work. I work on Qt Creator (Windows 7, 64 bits) and I've tried to change "Encodage par défaut" UTF-8, but it should not be here that I have to change choice.
I also want to cast wchar_t to char without doing something.
I had given up.
Do you know how I can do please ?
-
Hi,
I've already tried to print character such as beta or gamma and it doesn't work. I work on Qt Creator (Windows 7, 64 bits) and I've tried to change "Encodage par défaut" UTF-8, but it should not be here that I have to change choice.
I also want to cast wchar_t to char without doing something.
I had given up.
Do you know how I can do please ?
@Nauvisse
Are you talking about "printing" a character from your Qt program or entering it into Creator text editor?I also want to cast wchar_t to char without doing something.
Again, are you talking about at editing time (in which case I don't know what
wchar_t
or casting has to do with it) or in your code? -
Thank you for your reply. I had assumed I will get an email telling me a reply was sent on this forum. Sorry for my delay.
Printing character
Printing on the stdout, for example:
printf("\beta");
wchar_t
Here is my code. I was said on this forum https://learn.microsoft.com/en-us/answers/questions/1856800/incorrect-file-creation-date?page=1&orderby=Helpful&comment=answer-1644021#newest-answer-comment (clik comments to see the response) that "the project will build as unicode or non-unicode based on the project property settings"
void SetFileToCurrentTime(const char* pathDirFile) { HANDLE hFile; FILETIME ft; SYSTEMTIME st; size_t origsize = strlen(pathDirFile) + 1; const size_t newsize = mbstowcs(NULL,pathDirFile,origsize) + 1; wchar_t wPathDirFile[newsize]; mbstowcs(wPathDirFile,pathDirFile,origsize); hFile = CreateFile(wPathDirFile,FILE_WRITE_ATTRIBUTES,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL); if (hFile == INVALID_HANDLE_VALUE) {printf("Cannot create times of the file, error code is %lud\n",GetLastError()); return;} if (GetLastError() == ERROR_FILE_NOT_FOUND) {printf("Cannot create times of the file because ERROR_FILE_NOT_FOUND.\n"); return;} GetSystemTime(&st); if (!SystemTimeToFileTime(&st,&ft)) {printf("Cannot create times of the file because SystemTimeToFileTime failed.\n"); return;} if (!SetFileTime(hFile,&ft,&ft,&ft)) {printf("Cannot create times of the file because SetFileTime failed.\n"); return;} if (!CloseHandle(hFile)) {printf("Cannot create times of the file because CloseHandle failed.\n"); return;} }
Thank you
-
-
Your code is 100% Windows-specific and has nothing Qt in it. I don't know why you would try to use Qt with it.
Your screenshot shows settings for the Text Editor inside Qt Creator. Settings there (including encoding or UTF-8 or whatever) are for when you enter source code text into the editor. That has no relationship to
wchar_t
or similar which you use at runtime in your code. You ask aboutprintf("\beta");
. But you don't use anything like that in yourSetFileToCurrentTime()
implementation so I have no idea what the connection is. Incidentally"\beta"
is incorrect as a C++ literal string in source code, so that's not great. You say "I've already tried to print character such as beta or gamma and it doesn't work. ", but we don't know what "doesn't work" means. If you literally haveprintf("\beta");
then (a) that might be the reason for your "doesn't work" and (b) even if you correct to good C++printf()
is not going to output some "beta" character from that.Whatever your problem is about "printing characters" you do not say where you run your program, e.g. inside Creator versus outside, whether you use Creator's inbuilt terminal when you run it, etc.
All in all I don't know what your question or problem is, what it has to do with Qt/Creator, or why you are even using Qt/Creator.
-
We call
setlocale(LC_ALL, ".UTF8");
at the beginning inmain()
. This says that every Windows function treatschar*
as UTF-8 strings. There is a compiler switch that tells if functions likeCreateFile
get replaced by eitherCreateFileA
orCreateFileW
. If you choose theA
variant with this trick you can now provide UTF-8 file paths. Then you don't need any conversions anymore. Another good thing is that the code is then portable between Windows and all the other platforms (which already all use UTF-8).If you want to show Unicode characters on the console (e.g. by using
printf
) you need to make sure that the correct code page is set. Either your standard code page already supports Greek letters or you might want to switch to the UTF-8 code page. However, there is no way in C++ that you can write\beta
or\gamma
. You have to provide the character itself inside au8"..."
string or use Unicode escape codes.PS: This gives me the idea that we could actually try to overload the string operator to substitute
\beta
with the correct UTF-8 character. Has anyone ever tried this? Something like"\beta"_latex
...