Home > Blockchain >  Text not showing after printing UTF-8 in C
Text not showing after printing UTF-8 in C

Time:10-21

I'm trying to print some UTF-8 text on my program. It doesn't show up when I do the normal print function, but it did work after I used #include<fcntl.h>, #include <io.h>, setmode(_fileno(stdout), _O_U16TEXT);, and turning printf to wprintf. However, after I used those lines mentioned above, it made all of my normal print lines invisible, and only the UTF-8 texts visible. How do i fix this?

Here's my code:

#include<fcntl.h>
#include <io.h>
#include <stdio.h>

int main (){
setmode(_fileno(stdout), _O_U16TEXT); 

wprintf(L"░██████╗████████╗██╗░░░██╗██████╗░███████╗███╗░░██╗████████╗  ██╗░░░██╗░█████╗░████████╗██╗███╗░░██╗░██████╗░\n");printf("\n");
wprintf(L"██╔════╝╚══██╔══╝██║░░░██║██╔══██╗██╔════╝████╗░██║╚══██╔══╝  ██║░░░██║██╔══██╗╚══██╔══╝██║████╗░██║██╔════╝░\n");printf("\n");
wprintf(L"╚█████╗░░░░██║░░░██║░░░██║██║░░██║█████╗░░██╔██╗██║░░░██║░░░  ╚██╗░██╔╝██║░░██║░░░██║░░░██║██╔██╗██║██║░░██╗░\n");printf("\n");
wprintf(L"░╚═══██╗░░░██║░░░██║░░░██║██║░░██║██╔══╝░░██║╚████║░░░██║░░░  ░╚████╔╝░██║░░██║░░░██║░░░██║██║╚████║██║░░╚██╗\n");printf("\n");
wprintf(L"██████╔╝░░░██║░░░╚██████╔╝██████╔╝███████╗██║░╚███║░░░██║░░░  ░░╚██╔╝░░╚█████╔╝░░░██║░░░██║██║░╚███║╚██████╔╝\n");printf("\n");
wprintf(L"╚═╝░░░░╚═════╝░╚═════╝░╚══════╝╚═╝░░╚══╝░░░╚═╝░░░  ░░░╚═╝░░░░╚════╝░░░░╚═╝░░░╚═╝╚═╝░░╚══╝░╚═════╝░\n");printf("\n");
wprintf(L"░██████╗██╗░░░██╗░██████╗████████╗███████╗███╗░░░███╗\n");printf("\n");
wprintf(L"██╔════╝╚██╗░██╔╝██╔════╝╚══██╔══╝██╔════╝████╗░████║\n");printf("\n");
wprintf(L"╚█████╗░░╚████╔╝░╚█████╗░░░░██║░░░█████╗░░██╔████╔██║\n");printf("\n");
wprintf(L"░╚═══██╗░░╚██╔╝░░░╚═══██╗░░░██║░░░██╔══╝░░██║╚██╔╝██║\n");printf("\n");
wprintf(L"██████╔╝░░░██║░░░██████╔╝░░░██║░░░███████╗██║░╚═╝░██║\n");printf("\n");
wprintf(L"╚═════╝░░░░╚═╝░░░╚═════╝░░░░╚═╝░░░╚══════╝╚═╝░░░░░╚═╝\n");printf("\n"); 


printf("THIS BECOMES INVISIBLE");
printf("how do i make it visible???");

    return;
}

Thank you!

CodePudding user response:

Cannot easily mix wprintf() with printf():

Each stream has an orientation. After a stream is associated with an external file, but before any operations are performed on it, the stream is without orientation. Once a wide character input/output function has been applied to a stream without orientation, the stream becomes a wide-oriented stream. Similarly, once a byte input/output function has been applied to a stream without orientation, the stream becomes a byte-oriented stream. Only a call to the freopen function or the fwide function can otherwise alter the orientation of a stream. (A successful call to freopen removes any orientation.)
C2x § 7.21.2 4

CodePudding user response:

I think here your solution

#include <stdio.h>
#include <wchar.h>
#include <locale.h>

int main(void)
{
    setlocale(LC_ALL, "");
    printf("░██████╗████████╗██╗░░░██╗██████╗░███████╗███╗░░██╗████████╗  ██╗░░░██╗░█████╗░████████╗██╗███╗░░██╗░██████╗░\n");
    printf("██╔════╝╚══██╔══╝██║░░░██║██╔══██╗██╔════╝████╗░██║╚══██╔══╝  ██║░░░██║██╔══██╗╚══██╔══╝██║████╗░██║██╔════╝░\n");
    printf("╚█████╗░░░░██║░░░██║░░░██║██║░░██║█████╗░░██╔██╗██║░░░██║░░░  ╚██╗░██╔╝██║░░██║░░░██║░░░██║██╔██╗██║██║░░██╗░\n");
    printf("░╚═══██╗░░░██║░░░██║░░░██║██║░░██║██╔══╝░░██║╚████║░░░██║░░░  ░╚████╔╝░██║░░██║░░░██║░░░██║██║╚████║██║░░╚██╗\n");
    printf("██████╔╝░░░██║░░░╚██████╔╝██████╔╝███████╗██║░╚███║░░░██║░░░  ░░╚██╔╝░░╚█████╔╝░░░██║░░░██║██║░╚███║╚██████╔╝\n");
    printf("╚═╝░░░░╚═════╝░╚═════╝░╚══════╝╚═╝░░╚══╝░░░╚═╝░░░  ░░░╚═╝░░░░╚════╝░░░░╚═╝░░░╚═╝╚═╝░░╚══╝░╚═════╝░\n");
    printf("░██████╗██╗░░░██╗░██████╗████████╗███████╗███╗░░░███╗\n");
    printf("██╔════╝╚██╗░██╔╝██╔════╝╚══██╔══╝██╔════╝████╗░████║\n");
    printf("╚█████╗░░╚████╔╝░╚█████╗░░░░██║░░░█████╗░░██╔████╔██║\n");
    printf("░╚═══██╗░░╚██╔╝░░░╚═══██╗░░░██║░░░██╔══╝░░██║╚██╔╝██║\n");
    printf("██████╔╝░░░██║░░░██████╔╝░░░██║░░░███████╗██║░╚═╝░██║\n");
    printf("╚═════╝░░░░╚═╝░░░╚═════╝░░░░╚═╝░░░╚══════╝╚═╝░░░░░╚═╝\n"); 
    
    printf("THIS BECOMES INVISIBLE\n");
    printf("how do i make it visible???\n");
    return 0;
}

Output:

░██████╗████████╗██╗░░░██╗██████╗░███████╗███╗░░██╗████████╗  ██╗░░░██╗░█████╗░████████╗██╗███╗░░██╗░██████╗░
██╔════╝╚══██╔══╝██║░░░██║██╔══██╗██╔════╝████╗░██║╚══██╔══╝  ██║░░░██║██╔��═██╗╚══██╔══╝██║████╗░██║██╔════╝░
╚█████╗░░░░██║░░░██║░░░██║██║░░██║█████╗░░██╔██╗██║░░░██║░░░  ╚██╗░██╔╝██║░░██║░░░██║░░░██║██╔██╗██║██║░░██╗░
░╚═══██╗░░░██║░░░██║░░░██║██║░░██║██╔══╝░░██║╚██��█║░░░██║░░░  ░╚████╔╝░██║░░██║░░�██║░░░██║██║╚████║██║░░╚██╗
██████╔╝░░░██║░░░╚██████╔╝██████╔╝███████╗██║░╚███║░░░██║░░░  ░░╚██╔╝��░╚█████╔╝░░░██║░░░██║██║░╚███║╚��█████╔╝
╚═╝░░░░╚═════╝░╚═════╝░�══════╝╚═╝░░╚══╝░░░╚═╝░░░  ░░░╚═╝░░░░╚════╝░░░░╚═╝░░░╚═╝╚═╝░░╚══╝░╚═════╝░
░██████╗██╗░░░██╗░████��█╗████████╗███████╗███╗░░░███╗
�█╔════╝╚██╗░██╔╝██╔════╝╚══██╔═�╝██╔════╝████╗░████║
╚█████╗░░╚████╔╝░╚█████╗░░░░██║░░░█████╗░░██╔████╔██║
░╚═══██╗░░╚██╔╝░░░╚══��██╗░░░██║░░░██╔══╝░░██║╚██╔╝██║
██████╔╝░░░██║░░░██████╔╝░░░██║�░░███████╗██║░╚═╝░██║
╚═════╝░░░░╚═╝░░░╚═════╝░░░░╚═╝░░░╚══════╝╚═╝░░░░░╚═╝
THIS BECOMES INVISIBLE
how do i make it visible???

CodePudding user response:

Save the original mode when you change it, flush and restore the mode to switch back. You can only use wide stdout functions in _O_U16TEXT mode. See the two cautions in the docs:

Unicode mode is for wide print functions (for example, wprintf) and is not supported for narrow print functions. Use of a narrow print function on a Unicode mode stream triggers an assert.

and:

If you write data to a file stream, explicitly flush the code by using fflush before you use _setmode to change the mode. If you do not flush the code, you might get unexpected behavior. If you have not written data to the stream, you do not have to flush the code.

#include<fcntl.h>
#include <io.h>
#include <stdio.h>

int main () {
    auto old_mode = _setmode(_fileno(stdout), _O_U16TEXT);

    wprintf(L"░██████╗████████╗██╗░░░██╗██████╗░███████╗███╗░░██╗████████╗  ██╗░░░██╗░█████╗░████████╗██╗███╗░░██╗░██████╗░\n");
    wprintf(L"██╔════╝╚══██╔══╝██║░░░██║██╔══██╗██╔════╝████╗░██║╚══██╔══╝  ██║░░░██║██╔══██╗╚══██╔══╝██║████╗░██║██╔════╝░\n");
    wprintf(L"╚█████╗░░░░██║░░░██║░░░██║██║░░██║█████╗░░██╔██╗██║░░░██║░░░  ╚██╗░██╔╝██║░░██║░░░██║░░░██║██╔██╗██║██║░░██╗░\n");
    wprintf(L"░╚═══██╗░░░██║░░░██║░░░██║██║░░██║██╔══╝░░██║╚████║░░░██║░░░  ░╚████╔╝░██║░░██║░░░██║░░░██║██║╚████║██║░░╚██╗\n");
    wprintf(L"██████╔╝░░░██║░░░╚██████╔╝██████╔╝███████╗██║░╚███║░░░██║░░░  ░░╚██╔╝░░╚█████╔╝░░░██║░░░██║██║░╚███║╚██████╔╝\n");
    wprintf(L"╚═════╝░░░░╚═╝░░░░╚═════╝░╚═════╝░╚══════╝╚═╝░░╚══╝░░░╚═╝░░░  ░░░╚═╝░░░░╚════╝░░░░╚═╝░░░╚═╝╚═╝░░╚══╝░╚═════╝░\n");
    wprintf(L"░██████╗██╗░░░██╗░██████╗████████╗███████╗███╗░░░███╗\n");
    wprintf(L"██╔════╝╚██╗░██╔╝██╔════╝╚══██╔══╝██╔════╝████╗░████║\n");
    wprintf(L"╚█████╗░░╚████╔╝░╚█████╗░░░░██║░░░█████╗░░██╔████╔██║\n");
    wprintf(L"░╚═══██╗░░╚██╔╝░░░╚═══██╗░░░██║░░░██╔══╝░░██║╚██╔╝██║\n");
    wprintf(L"██████╔╝░░░██║░░░██████╔╝░░░██║░░░███████╗██║░╚═╝░██║\n");
    wprintf(L"╚═════╝░░░░╚═╝░░░╚═════╝░░░░╚═╝░░░╚══════╝╚═╝░░░░░╚═╝\n");

    fflush(stdout);
    _setmode(_fileno(stdout), old_mode);
    printf("This is no longer invisible\n");
}

Output:

░██████╗████████╗██╗░░░██╗██████╗░███████╗███╗░░██╗████████╗  ██╗░░░██╗░█████╗░████████╗██╗███╗░░██╗░██████╗░
██╔════╝╚══██╔══╝██║░░░██║██╔══██╗██╔════╝████╗░██║╚══██╔══╝  ██║░░░██║██╔══██╗╚══██╔══╝██║████╗░██║██╔════╝░
╚█████╗░░░░██║░░░██║░░░██║██║░░██║█████╗░░██╔██╗██║░░░██║░░░  ╚██╗░██╔╝██║░░██║░░░██║░░░██║██╔██╗██║██║░░██╗░
░╚═══██╗░░░██║░░░██║░░░██║██║░░██║██╔══╝░░██║╚████║░░░██║░░░  ░╚████╔╝░██║░░██║░░░██║░░░██║██║╚████║██║░░╚██╗
██████╔╝░░░██║░░░╚██████╔╝██████╔╝███████╗██║░╚███║░░░██║░░░  ░░╚██╔╝░░╚█████╔╝░░░██║░░░██║██║░╚███║╚██████╔╝
╚═════╝░░░░╚═╝░░░░╚═════╝░╚═════╝░╚══════╝╚═╝░░╚══╝░░░╚═╝░░░  ░░░╚═╝░░░░╚════╝░░░░╚═╝░░░╚═╝╚═╝░░╚══╝░╚═════╝░
░██████╗██╗░░░██╗░██████╗████████╗███████╗███╗░░░███╗
██╔════╝╚██╗░██╔╝██╔════╝╚══██╔══╝██╔════╝████╗░████║
╚█████╗░░╚████╔╝░╚█████╗░░░░██║░░░█████╗░░██╔████╔██║
░╚═══██╗░░╚██╔╝░░░╚═══██╗░░░██║░░░██╔══╝░░██║╚██╔╝██║
██████╔╝░░░██║░░░██████╔╝░░░██║░░░███████╗██║░╚═╝░██║
╚═════╝░░░░╚═╝░░░╚═════╝░░░░╚═╝░░░╚══════╝╚═╝░░░░░╚═╝
This is no longer invisible
  • Related