Problem with [colortext()] and [textbackground()]

This is a discussion on Problem with [colortext()] and [textbackground()] within the A Brief History of Cprogramming.com forums, part of the Community Boards category; Why does [cprintf()] not work the same way as [printf()]? Please compile my code and see what I mean. Code: ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    43

    Exclamation Problem with [colortext()] and [textbackground()]

    Why does [cprintf()] not work the same way as [printf()]?

    Please compile my code and see what I mean.

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int main(void)
    {
    
          printf("1.          This is the normal dos colors.\n");
          printf("2.          This is right below.\n");
          printf("3.          This is under \"This is right below.\"\n");
          printf("4.          and this is the last part\n\n");
    
    
          textcolor(BLUE);
          cprintf("1.          This should be the color: BLUE.");
    
          printf("2.          This should be right below the BLUE text.");
    
          textcolor(LIGHTGRAY);
          textbackground(BLUE);
          cprintf("3.          This should have a BLUE background.");
    
          getchar();
    
    return 0;
    }
    ()()() MonKey ware
    Kyle J. R.

  2. #2
    Registered User alex's Avatar
    Join Date
    Sep 2001
    Posts
    132

    Re: Problem with [colortext()] and [textbackground()]

    Originally posted by Goof Program
    Why does [cprintf()] not work the same way as [printf()]?
    (Borlands) cprintf does not use dos to write to the screen, but writes directly to video memory (or uses bios calls, depending on a global variable). This makes it faster and also introduces the possibility to use color efficiently. Dos has no provision for using color. The output of cprintf cannot be redirected to a file (using | or > on the dos command line). There is another difference: cprintf interprets "\n" as a line feed only, while printf automatically prepends a carriage return ("\r").

    Hope this helps...
    alex

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    43

    Question

    I have one last question. If I do [cprintf("text here\r");] is that the same as [printf("text here\n");]?

    Thanks
    ()()() MonKey ware
    Kyle J. R.

  4. #4
    Registered User alex's Avatar
    Join Date
    Sep 2001
    Posts
    132
    Almost...

    cprintf("text here\r\n");
    should look the same as
    printf("text here\n");
    except for the color of course.

    greetinx,
    alex

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    43

    Smile

    Thanks for the help, It has made my coding easyer.
    ()()() MonKey ware
    Kyle J. R.

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    59

    Screen Background Color

    How would I go about making a full screen program in DOS that has a specified Background color and all that good stuff... not just text colors and text backgrounds?

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    59

    Back To Original

    Is there any way to go back to the original DOS colors (light grey and black) without manually redefining textbackground and text color and placing another cprintf() function in the program?

  8. #8
    I'm Back
    Join Date
    Dec 2001
    Posts
    556
    i use the foll statements to do that

    ---
    ---
    textcolor(7); /orig colors
    clrscr();
    ---
    ---

    Also, the "cls" command puts it back to normal

  9. #9
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    The main difference which should answer all your questions, is that in ASM, there's text (usually int 21), and video (a WHOLE bunch of other stuff), which is more specialized to graphics. cprintf uses video, and printf uses the text, so if you need something special that neither of these offer, the ASM is where it all comes from.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21