Unicode symbols - c

I wrote the following code to generate Unicode symbols for card suits in C. It works fine and I don't need to change the font family or the code page of the console (I use Windows 10 and Dev-C++) but it seems that I can generate only those symbols. Indeed, if I try other values to generate other symbols, like for chess, dice, domino or others as shown here unicode symbols for games, that code does not work. Why? Thank you in advance for your help.
#include <stdio.h>
#include <fcntl.h>
#define SPADE L"\u2660"
#define CLUB L"\u2663"
#define HEART L"\u2665"
#define DIAMOND L"\u2666"
enum SUIT {spade = 1, club, heart, diamond};
void printSuit(int suitToSelect) {
_setmode(_fileno(stdout), _O_U16TEXT);
switch (suitToSelect) {
case spade:
wprintf(SPADE);
break;
case club:
wprintf(CLUB);
break;
case heart:
wprintf(HEART);
break;
case diamond:
wprintf(DIAMOND);
break;
}
_setmode(_fileno(stdout), _O_TEXT);
}
int main(void)
{
printSuit(spade);
printSuit(heart);
printSuit(club);
printSuit(diamond);
printf("\n");
printf("Normal text\n");
return 0;
}

the code below works fine with Dev-C++ and Windows 10.
You can see the nice result in the screenshot of the console.
You can select different fonts in the code
to visualize unicode symbols; each font family
prints the symbols in different styles. Try it!
I hope that this answer can help somebody
who had my problem.
Thank you.
#include <windows.h>
#include <stdio.h>
int main(void)
{
CONSOLE_FONT_INFOEX cfi;
cfi.cbSize = sizeof cfi;
cfi.nFont = 0;
cfi.dwFontSize.X = 20;
cfi.dwFontSize.Y = 40;
cfi.FontFamily = FF_DONTCARE;
cfi.FontWeight = FW_NORMAL;
wcscpy(cfi.FaceName, L"Dejavu Sans Mono");
//wcscpy(cfi.FaceName, L"MS Gothic");
//wcscpy(cfi.FaceName, L"MS Mincho");
//wcscpy(cfi.FaceName, L"NSimSun");
SetCurrentConsoleFontEx(GetStdHandle(STD_OUTPUT_HANDLE), FALSE, &cfi);
SetConsoleOutputCP(65001);
printf("\u2660 \u2661 \u2662 \u2663 \u2664 \u2665 \u2666 \u2667 \n");
printf("\u265B \u265C \u265D \u265E\n");
printf("\u265F \u265A \u2655 \u2656 \u2657\n");
printf("\u2658 \u2659 \u2654 \n");
printf("\u2680 \u2681 \u2682 \u2683 \u2684 \u2685\n");
printf("\u2554 \u2555 \u2556 \u2563 \u255A \u255B \u255C \u255D\n");
return 0;
}

Related

How do I can call initgraph more than one time?

Please look at the following code:
#include <stdio.h>
#include <graphics.h>
#include <conio.h>
using namespace std;
void drawrect()
{
int gdriver = IBM8514, gmode;
initgraph(&gdriver, &gmode, "");
rectangle(500, 500, 700, 700);
getch();
cleardevice();
closegraph();
}
int main()
{
int f=1;
while(f)
{
char c;
printf("Press \"e\" to end, and any other character to draw a rectangle");
scanf("%c",&c);
c=='e' ? f=0:f=1;
drawrect();
fflush(stdin);
}
}
at the first time when I run this program, It works correctly and draws a rectangle, but after the first time, the rectangle function doesn't work and the GUI screen is completely blank, While I've cleared and closed previous graphic
So why it doesn't work at second time?
You code has undefined behaviour. The call to initgraph
int gdriver = IBM8514, gmode;
initgraph(&gdriver, &gmode, "");
should pass a pointer to the graphics mode you want to use. This page describes the function and its arguments, and about the mode it says:
*graphmode is an integer that specifies the initial graphics mode (unless *graphdriver equals DETECT; in which case, *graphmode is set
by initgraph to the highest resolution available for the detected
driver). You can give *graphmode a value using a constant of the
graphics_modes enumeration type, which is defined in graphics.h and
listed below.
graphdriver and graphmode must be set to valid values from the
following tables, or you will get unpredictable results. The exception
is graphdriver = DETECT.
But you have not set the mode, and as the second paragraph quoted says, the result is unpredictable. This can be: working how you intended, not working, working strangely, or frying the processor.
So set the graphics mode you want to use with say
int gdriver = IBM8514, gmode = 0;
or whatever mode you need to use. Alternatively you can tell the system to detect for itself, in which case you can use
int gdriver = DETECT, gmode;
Init and close should be called just once and not be called in the drawrect but usually in the main instead ... also having getch in rendering routine makes no sense too...
I will not touch other issues here of your code as I am not coding console stuff for years and BGI even longer but I would start with reordering the code to this:
#include <stdio.h>
#include <graphics.h>
#include <conio.h>
using namespace std;
void drawrect()
{
rectangle(500, 500, 700, 700);
}
int main()
{
int f=1;
int gdriver = IBM8514, gmode;
initgraph(&gdriver, &gmode, "");
while(f)
{
char c;
printf("Press \"e\" to end, and any other character to draw a rectangle");
scanf("%c",&c);
c=='e' ? f=0:f=1;
drawrect();
getch();
fflush(stdin);
}
cleardevice();
closegraph();
}
Also in future address the library by its real name BGI because graphics.h has no meaning as almost all gfx api/libs got a file with that name ...

How to do a ansi color codes

The problem is that when i typed
printf("\033[1;32mHello World\033[0m");
it prints something like this
[1;32mHello World[0m
in the console. My code is
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int main(){
printf("\033[1;32mHello World\033[0m");
_getch();
return 0;
}
it displays:
a box with a question mark inside->[1;32mHello World[0m
but should be a color green text color Hello World.
Maybe in this way:
HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD dwMode = 0;
GetConsoleMode(hStdout, &dwMode);
dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
SetConsoleMode(hStdout, dwMode);
printf("\033[1;32mHello World\033[0m");
You need to initialize the appropriate mode Windows console.

Accessing custom XResources colors with X11

I'm just starting my first ever program using the X11 library. To start, I'm just trying to access the colors from the user's color-scheme as defined in xrdb. For example, in my ~/.Xresources I have things like:
*color8: #073642
*color0: #002b36
I've also verified that these colors show up when I run xrdb -query. In my C program so far I have:
#include <X11/Xlib.h>
#include <X11/Xresource.h>
int main (int argc, char *argv[])
{
Display* display = XOpenDisplay (0);
XrmDatabase xrdb = XrmGetDatabase (display);
XrmValue v;
Colormap cmap = DefaultColormap (display, DefaultScreen (display));
XColor screenColor;
XColor exactColor;
if (! XAllocNamedColor (display, cmap "color0", &screenColor, &exactColor))
printf ("ERROR\n");
printf ("%u %u %u\n", screenColor.red, screenColor.green, screenColor.blue);
return 0;
}
But this errors. So what am I missing? Is there a better way to do what I'm trying to do? Thanks!
When you want to access parameters set in an Xresource file loaded by xrdb, you need to
xrdb = XrmGetStringDatabase(XResourceManagerString(display));
instead of XrmGetDatabase(...). Hope that addresses (lately) your question.

File Finder in C

First of all Sorry for my bad English. I am not native English.
I am going to write a program that list all available logical disk drives. Then ask the user to select a drive. then takes a file extension and searches that file type in given drive (including directories and sub-directories). Program should be able to run on windows xp and onward. It should be single stand alone application. I am not expert in C. I have some hands on C#. i have following questions in this regard.
1. Is there any IDE/Tool in which i can write C# like code that directly compiles to single stand alone application for windows?
2. Can you recommend some libs that i can use state forward for this purpose like using in C#? (I have seen dirent and studying it.)
I coppied some code that i am testing as a startup.
#include <windows.h>
#include <direct.h>
#include <stdio.h>
#include <tchar.h>
#include<conio.h>
#include<dirent.h>
//------------------- Get list of all fixed drives in char array. only drive letter is get. not path.
// add ":\" to build path.
char AllDrives[26];
DWORD GetAllDrives()
{
int AvlDrives=0;
DWORD WorkState=-1;
//TCHAR DrivePath[] = _T("A:\\"); //Orignal Type
//start getting for drive a:\ to onward.
char DrivePath[] = {"A:\\"};
//http://www.tenouk.com/cpluscodesnippet/getdrivetype.html
ULONG uDriveMask = _getdrives();
if (uDriveMask == 0)
{
WorkState=GetLastError();
printf("\r\nFailed to Get drives. Error Details : %lu", WorkState);
return WorkState;
}
else
{
WorkState=0xFF;
printf("The following logical drives are being used:\n");
while (uDriveMask) {
if (uDriveMask & 1)
{
UINT drvType=0;
drvType = GetDriveType(DrivePath);
if(drvType==3)
{
AllDrives[AvlDrives]= DrivePath[0];
AvlDrives++;
printf("\r\n%s",DrivePath);
}
}
++DrivePath[0]; //Scan to all scanable number of drives.
uDriveMask >>= 1;
}
}
return WorkState;
}
int main(int argc, char* argv[]) {
char DrivePath[]={"C:\\"};
char CurrentDrive[]={"C:\\"};
DWORD Drives=-1;
int d=0;
for( d=0; d<26; d++)
AllDrives[d]=' ';
Drives=GetAllDrives();
if(Drives >0)
{
int Length= sizeof(AllDrives);
for(int x=0; x<26; x++)
{
if(AllDrives[x]!=' ')
{
printf("\r\nFixed Drive : %c",AllDrives[x]);
}
}
}
getch();
}
You can use visual studio compiler to compile C and C++ in Windows, and it is available with its own IDE. When you install visual studio, it will install required libraries also to compile the C/C++ program. There are other IDEs and compilers available compatible with Windows like DevC++,CodeBlocks.

C get battery life under Windows 7

I'm trying to code a program that gets the % of a laptop battery and then displays a CMD showing a message (for example: 10% -> "Low battery!").
I've tried to google it, and it seems they all tried with C++ or C#.
Can anybody help me with C, please?
Edit: thanks zakinster for your reply. Shouldn't it look something like this? This code ain't working.
#include <Windows.h>
#include <Winbase.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {
SYSTEM_POWER_STATUS status;
GetSystemPowerStatus(&status);
unsigned char battery = status.BatteryLifePercent;
printf("%s", battery);
}
GetSystemPowerStatus from the Win32 API should provide the information you need :
SYSTEM_POWER_STATUS status;
if(GetSystemPowerStatus(&status)) {
unsigned char battery = status.BatteryLifePercent;
/* battery := 0..100 or 255 if unknown */
if(battery == 255) {
printf("Battery level unknown !");
}
else {
printf("Battery level : %u%%.", battery);
}
}
else {
printf("Cannot get the power status, error %lu", GetLastError());
}
See the documentation of the SYSTEM_POWER_STATUS structure for a complete list of contained information.

Resources