color printf with Bloodshed - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

color printf with Bloodshed

Hi

I would like to printf with some color. I am using Bloodshed Software - Dev-C++, and I have seen some code online for coloring output, but nothing seems to work with bloodshed. At school, we use bloodshed so for now I want to learn to do thing on this software. Keep in mind that I am linux user, which means g++, gcc.

Following code I found online is working with linux, but it is not working with bloodshed. My teacher told me that bloodshed has some limitation, missing some features. Is there a very simple color code that will work with bloodshed?

All I want to do is color the output of printf("hello, world
");

Thanks in advance.

[code]
#include

#define RESET 0
#define BRIGHT 1
#define DIM 2
#define UNDERLINE 3
#define BLINK 4
#define REVERSE 7
#define HIDDEN 8

#define BLACK 0
#define RED 1
#define GREEN 2
#define YELLOW 3
#define BLUE 4
#define MAGENTA 5
#define CYAN 6
#define WHITE 7

void textcolor(int attr, int fg, int bg);
int main()
{ textcolor(BRIGHT, RED, BLACK);
printf("In color
");
textcolor(RESET, WHITE, BLACK);
return 0;
}

void textcolor(int attr, int fg, int bg)
{ char command[13];

/* Command is the control command to the terminal */
sprintf(command, "%c[%d;%d;%dm", 0x1B, attr, fg + 30, bg + 40);
printf("%s", command);
}


[/code]


Comments

  • pentaclepentacle Posts: 36Member
    : Hi
    :
    : I would like to printf with some color. I am using Bloodshed
    : Software - Dev-C++, and I have seen some code online for coloring
    : output, but nothing seems to work with bloodshed. At school, we use
    : bloodshed so for now I want to learn to do thing on this software.
    : Keep in mind that I am linux user, which means g++, gcc.
    :
    : Following code I found online is working with linux, but it is not
    : working with bloodshed. My teacher told me that bloodshed has some
    : limitation, missing some features. Is there a very simple color code
    : that will work with bloodshed?
    :
    : All I want to do is color the output of printf("hello, world
    ");
    :
    : Thanks in advance.
    :
    : [code]:
    : #include
    :
    : #define RESET 0
    : #define BRIGHT 1
    : #define DIM 2
    : #define UNDERLINE 3
    : #define BLINK 4
    : #define REVERSE 7
    : #define HIDDEN 8
    :
    : #define BLACK 0
    : #define RED 1
    : #define GREEN 2
    : #define YELLOW 3
    : #define BLUE 4
    : #define MAGENTA 5
    : #define CYAN 6
    : #define WHITE 7
    :
    : void textcolor(int attr, int fg, int bg);
    : int main()
    : { textcolor(BRIGHT, RED, BLACK);
    : printf("In color
    ");
    : textcolor(RESET, WHITE, BLACK);
    : return 0;
    : }
    :
    : void textcolor(int attr, int fg, int bg)
    : { char command[13];
    :
    : /* Command is the control command to the terminal */
    : sprintf(command, "%c[%d;%d;%dm", 0x1B, attr, fg + 30, bg + 40);
    : printf("%s", command);
    : }
    :
    :
    : [/code]:
    :
    :
    :

    [color=Green][size=3][b]textcolor()[/b] is defined in the header file [b]conio.h[/b] which is non-portable (compiler specific). Turbo C/C++ has [b]conio.h[/b] as one of its header files but I'm not sure whether it is also included in Dev-C++.[/size][/color]
    [color=Black][size=3][code]
    #include

    int main()
    {
    int i;
    clrscr();

    for(i=0; i<=15; i++)
    {
    textcolor(i);
    cprintf("Hello World

    ");
    }

    return 0;
    }
    [/code][/size][/color]
  • alishadevochkaalishadevochka Posts: 6Member
    : : Hi
    : :
    : : I would like to printf with some color. I am using Bloodshed
    : : Software - Dev-C++, and I have seen some code online for coloring
    : : output, but nothing seems to work with bloodshed. At school, we use
    : : bloodshed so for now I want to learn to do thing on this software.
    : : Keep in mind that I am linux user, which means g++, gcc.
    : :
    : : Following code I found online is working with linux, but it is not
    : : working with bloodshed. My teacher told me that bloodshed has some
    : : limitation, missing some features. Is there a very simple color code
    : : that will work with bloodshed?
    : :
    : : All I want to do is color the output of printf("hello, world
    ");
    : :
    : : Thanks in advance.
    : :
    : : [code]: :
    : : #include
    : :
    : : #define RESET 0
    : : #define BRIGHT 1
    : : #define DIM 2
    : : #define UNDERLINE 3
    : : #define BLINK 4
    : : #define REVERSE 7
    : : #define HIDDEN 8
    : :
    : : #define BLACK 0
    : : #define RED 1
    : : #define GREEN 2
    : : #define YELLOW 3
    : : #define BLUE 4
    : : #define MAGENTA 5
    : : #define CYAN 6
    : : #define WHITE 7
    : :
    : : void textcolor(int attr, int fg, int bg);
    : : int main()
    : : { textcolor(BRIGHT, RED, BLACK);
    : : printf("In color
    ");
    : : textcolor(RESET, WHITE, BLACK);
    : : return 0;
    : : }
    : :
    : : void textcolor(int attr, int fg, int bg)
    : : { char command[13];
    : :
    : : /* Command is the control command to the terminal */
    : : sprintf(command, "%c[%d;%d;%dm", 0x1B, attr, fg + 30, bg + 40);
    : : printf("%s", command);
    : : }
    : :
    : :
    : : [/code]: :
    : :
    : :
    : :
    :
    : [color=Green][size=3][b]textcolor()[/b] is defined in the header
    : file [b]conio.h[/b] which is non-portable (compiler specific). Turbo
    : C/C++ has [b]conio.h[/b] as one of its header files but I'm not sure
    : whether it is also included in Dev-C++.[/size][/color]
    : [color=Black][size=3][code]:
    : #include
    :
    : int main()
    : {
    : int i;
    : clrscr();
    :
    : for(i=0; i<=15; i++)
    : {
    : textcolor(i);
    : cprintf("Hello World

    ");
    : }
    :
    : return 0;
    : }
    : [/code]: [/size][/color]

    Hi

    It works in Linux, but not in Bloodshed 4.9.9.2.
  • LundinLundin Posts: 3,711Member
    You have to use the Win API function SetConsoleTextAttribute(). The Win API is so much better than non-standard conio, since the Win API is portable between Windows compilers.
Sign In or Register to comment.