TCHAR characters are not displayed correctly - c

I have a simple code and argv[1] is "Привет".
#include <stdio.h>
#include <tchar.h>
#include <Windows.h>
#include <locale.h>
int _tmain(int argc, TCHAR* argv[])
{
TCHAR buf[100];
_fgetts(buf, 100, stdin);
_tprintf(TEXT("\nargv[1] %s\n"), argv[1]);
_tprintf(TEXT("%s\n"), buf);
}
In the console, I write "Мир" and have this result:
If I use setlocale(LC_ALL, ""), I have this result:
What should I do to get the correct string in both cases?

Evidently your program works, except it cannot print correctly on the console window. This is because Windows console is not fully compatible with Unicode. Use _setmode for Visual Studio. This should work for Russian but there could be additional problems with some Asian languages. Use WriteConsole for other compilers.
Visual Studio Example:
#include <stdio.h>
#include <io.h> //for _setmode
#include <fcntl.h> //for _O_U16TEXT
int wmain(int argc, wchar_t* argv[])
{
_setmode(_fileno(stdout), _O_U16TEXT);
wprintf(L"%s", L"Привет\n");
return 0;
}

Related

how to use unicode blockelements in C?

I want to use unicode blocks in my C program to display them in the console like ▇, ░ and so on. However, whenever I try to use the escape sequence for unicode characters, I only get weird letters like:
printf("/u259A"); //259A is the unicode for ▚
Output: ÔûÜ
I looked up how to include unicode charactes then tried to use wchar_t:
#include <locale.h>
#include <stdio.h>
int main(int argc, char const *argv[]) {
setlocale(LC_ALL, "");
wchar_t c = "\u259A";
printf("%c",c);
return 0;
}
but that only gave me ☺ as the output instead of ▚. Removing setlocale() would give me a blank output. I dont know what do to from this point on. The only thing I saw was using printf("\xB2"); which gave you ▓. But I dont understand where the B2 comes from or what it stands for.
So what worked for me was the following:
#include <stdio.h>
#include <fcntl.h>
#include <io.h>
int main(int argc, char const *argv[]) {
_setmode(_fileno(stdout), _O_U16TEXT);
wprintf(L"\x2590 \x2554 \x258c \x2592"); //Output : ▐ ╔ ▌ ▒
return 0;
}
the function _setmode() is apparently for setting the console on u16 text encoding. wprintf() allows you to print wide characters (unicode aswell). The L"" before the string indicates to the compiler, that the following string is a unicode string. Thanks to everyone for their time and answers!

Termcap "cl" command doesn't clear screen

I can't seem to get termcap's "cl" command to work, but the terminal escape code does.
For example:
#include <termcap.h>
#include <stdio.h>
int main()
{
tputs(tgetstr("cl", NULL), 1, putchar);
}
This doesn't change the terminal. But when I run:
#include <stdio.h>
int main()
{
printf("\e[2J");
}
or if I call echo `tput cl`
The terminal is cleared.
Why does this happen? Shouldn't termcap give that same escape code?
EDIT: Fixed writing characters
EDIT2: It's because i didn't call tgetent() before calling tgetstr(). Thanks guys!
Before interrogating with tgetstr(), you need to find the description of the user's terminal with tgetent():
#include <stdio.h>
#include <stdlib.h> // getenv
#include <termcap.h> // tgetent tgetstr
int main(void)
{
char buf[1024];
char *str;
tgetent(buf, getenv("TERM"));
str = tgetstr("cl", NULL);
fputs(str, stdout);
return 0;
}
Compile with -ltermcap

How can I print card suit characters in C Win32 console application?

I have seen a few questions on how to print these characters but none of the methods appear to be working. I suspect it is because I making a Win32 console application based on some of the comments I read.
Here is an example of what I have tried in my code currently. It only prints question mark boxes, or if I change it around I get question marks or random symbols.
I have tried defining these at the top.
#define SPADE '\x06'
#define CLUB '\x05'
#define HEART '\x03'
#define DIAMOND '\x04'
inside function, these are some of the things I've tried. I have left S,D,H,C in case I can't figure it out.
printf("%lc", SPADE);
//printf("♠");
//printf("S");
printf("%lc", HEART);
//printf("♥");
//printf("H");
printf("%lc", DIAMOND);
//printf("♦");
//printf("D");
printf("%lc", CLUB);
//printf("♣");
//printf("C");
UTF-16 wchar_t and wide characters functions are needed in Windows.
#include <windows.h>
int main()
{
DWORD n;
HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);
const wchar_t *buf = L"♠♥♦♣\n";
WriteConsoleW(hout, buf, wcslen(buf), &n, 0);
return 0;
}
The following code will compile with Visual Studio:
#include <stdio.h>
#include <io.h> //for _setmode
#include <fcntl.h> //for _O_U16TEXT
int main()
{
_setmode(_fileno(stdout), _O_U16TEXT);
wprintf(L"♠♥♦♣\n");
return 0;
}
After setting the mode to UTF-16, you have to call _setmode(_fileno(stdout), _O_TEXT) if you wish to use printf again.

execvpe implicit declaration error

I'm testing the execvpe() in c, I tried the below code, which cause the error as "implicit declaration of function 'execvpe' is invalid in C99 [-Wimplicit-function-declaration]".
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define _GNU_SOURCE
#include <unistd.h>
int main(int argc, char const *argv[])
{
//execl("/bin/echo", "echo", "Hello, world", NULL);
char *path = getenv("PATH");
char pathenv[strlen(path) + sizeof("PATH=")];
sprintf(pathenv, "PATH=%s", path);
char *envp[] = {pathenv, NULL};
char *tests[] = {"ls", "-lR", NULL};
execvpe(tests[0], tests, envp);
fprintf(stderr, "failed to execute \"%s\"\n", tests[0]);
return 0;
}
Then I test this code as below to test the existing status (which I copied from Compiler warnings for execvpe function, this time no error. Is there anyone can help me to figure out what's wrong in my above code? Thanks!
#include <unistd.h>
extern int execvpe(const char *file, char *const argv[], char *const envp[]);
Move the #define _GNU_SOURCE directive to before any of the #include directives, e.g.
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
In Glibc, all of these headers pull in features.h which sets various macros based on the setting of _XOPEN_SOURCE, _POSIX_SOURCE, _GNU_SOURCE, etc. At the time of the first include, it is not set. When you get down to unistd.h, features.h has already been included and won't be applied again.

Visual C++ or C play sounds

I'm developping an easy game written in C (Visual C++)
and I want to know if there is a way to play sounds,
thanks
(I'm using Visual Studio)
Take a look at the PlaySound() function.
If you call PlaySound() with the SND_ASYNC flag, the function returns immediately after beginning the sound.
For example:
#include <windows.h>
#include <mmsystem.h>
PlaySound(L"test.wav", NULL, SND_ASYNC | SND_FILENAME);
You'll also have to add Winmm.lib in your project settings.
Here's a quick example that should work:
#pragma once
#include "stdafx.h"
#include <windows.h>
#include <mmsystem.h>
int _tmain(int argc, _TCHAR* argv[])
{
if(!PlaySound(L"test.wav", NULL, SND_ASYNC | SND_FILENAME))
printf("error\n");
else
printf("ok\n");
getch();
return 0;
}
and in stdafx.h:
#pragma once
#include <iostream>
#include <tchar.h>
#include <conio.h>

Resources