简体   繁体   中英

Read and print special ascii characters from file in c

I want to know how to read special ascii character from txt file.

I have this text file and I want to print:

▄ ▀█   █     █   █▄    █▀
████  █   █  ███    █   ▄███▄ ██▄▄▄ ████▄

How can this be printed? When go print it I get error. How fix?

I want to make mainmenu for game. Instead of print directly I want to type text file for flexibility. It supposed to print out "ASCII BASTILLE DAY" with 219th char but it too much of mess. I also try use batch file but it also does not work.

Here is the code:

void mainmenu()
{
    while (1) {
        if (mmplso <= 0) { PlaySound(TEXT("SFX\\VLVce.wav"), NULL, SND_ASYNC); mmplso = 50800; }
        cls();
        hidcur();


        /*
        printf("\n              Made by Mauser\n\n%d\n\n                VER(1.0A)\n\n", mmplso);
        //
        //
        // 
        //
        printf("\n       %c%c%c    %c%c%c  %c%c%c  %c%c%c%c%c %c%c%c%c%c\n", (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219);
        printf("      %c   %c  %c    %c   %c   %c     %c\n", (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219);
        printf("      %c%c%c%c%c   %c%c  %c       %c     %c\n", (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219);
        printf("      %c   %c %c   %c %c   %c   %c     %c\n", (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219);
        printf("      %c   %c  %c%c%c   %c%c%c  %c%c%c%c%c %c%c%c%c%c\n\n", (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219);
        //
        printf("%c%c%c%c   %c%c%c    %c%c%c %c%c%c%c%c %c%c%c%c%c %c     %c     %c%c%c%c%c\n", (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219,
            (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219);
        printf("%c   %c %c   %c  %c      %c     %c   %c     %c     %c\n", (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219);
        printf("%c%c%c%c  %c%c%c%c%c   %c%c    %c     %c   %c     %c     %c%c%c%c\n", (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219);
        printf("%c   %c %c   %c %c   %c   %c     %c   %c     %c     %c\n", (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219);
        printf("%c%c%c%c  %c   %c  %c%c%c    %c   %c%c%c%c%c %c%c%c%c%c %c%c%c%c%c %c%c%c%c%c\n\n", (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219), (char)219, (char)219, (char)219, (char)219, (char)219;
        //
        printf("            %c%c%c%c   %c%c%c  %c   %c\n", (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219);
        printf("            %c   %c %c   %c %c   %c\n", (char)219, (char)219, (char)219, (char)219, (char)219, (char)219);
        printf("            %c   %c %c%c%c%c%c  %c %c\n", (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219);
        printf("            %c   %c %c   %c   %c\n", (char)219, (char)219, (char)219, (char)219, (char)219);
        printf("            %c%c%c%c  %c   %c   %c\n\n\n", (char)219, (char)219, (char)219, (char)219, (char)219, (char)219, (char)219);
        //
        */


        printf("PRESS ANY KEY...");
        if (_kbhit()) { PlaySound(TEXT("SFX\\close.wav"), NULL, SND_ASYNC); break; }

        mmplso--;
    }
    system("cls");
}

void mainLoop()
{
    mmplso = 0;
    mainmenu();
    while (1)
    {
        Setup();
        mainGame();
    }
}

int main()
{
    SetConsoleCP(437);
    SetConsoleOutputCP(437);
    mainLoop();
    return 0;
}

```


Problem is most likely not in the code but in the way you create your text file. When you copy charters you are using (like ▀█▄) from the web and paste it in the default Windows notepad it's using UTF-8 encoding which is UNICODE and what you need is extended ASCII. So, first off I recommend you to use notepad++ and manually set code page of new text file to something like OEM 852. In notepad++ you do it like this:

Encoding > Character Set > Central European > OEM 852

After that , when you paste your special characters in the notepad++, text file they will be saved as ASCII and not UNICODE.

After you have correctly created text file you can print it to console by calling function that looks like this:

void printMyFile(const char *fileName) {
    FILE *file = fopen(fileName, "r");
    fseek(file, 0L, SEEK_END);
    long size = ftell(file);
    fseek(file, 0L, SEEK_SET);
    char *buffer = (char *)calloc(size, sizeof(char));
    fread(buffer, sizeof(char), size, file);
    fclose(file);
    printf(buffer);
    free(buffer);
}

Additionally if your console still show your characters wrongly you should include Windows.h

#include <Windows.h>

And call function

SetConsoleOutputCP(852);

once before you print anything to the console.
Wish you luck!

EDIT 1: I saw your new edit with your code, using SetConsoleOutputCP(437) is also ok and will work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM