0

im trying to print a interface using these characters:

"╣║╗╝╚╔╩╦╠═╬"

but, when i try to print it, returns something like this:

"ôöæËÈ"

interface.txt

unsigned char* tabuleiroImportado() {
     std::ifstream TABULEIRO;

     TABULEIRO.open("tabuleiro.txt");

     unsigned char tabu[36][256];

     for (unsigned char i = 0; i < 36; i++) {
          TABULEIRO >> tabu[i];
          std::cout << tabu[i] << std::endl;
     }
     return *tabu;
}

i'm using this function to import the interface.

  • 1
    The terminal you're using to display the characters may use a different encoding. In the encoding your program expects, the code is rendered as ╣, but the to the terminal the same code means ô. – user4581301 Jan 20 '22 at 01:04
  • 1
    You are returning a pointer to a local variable that is destroyed when the function exits, thus the caller acts on a *dangling pointer to invalid memory*, which is **undefined behavior**. Consider returning a `std::vector` or a `std::array, 36>` instead, so the data gets copied to the caller. Or else have the caller allocate the array and pass a pointer into the function to fill that array. – Remy Lebeau Jan 20 '22 at 01:11
  • i'm assigning this pointer to another pointer in my main function, something like: char* tabu = tabuleiroImportado();, is that a problem? – Gabriel Vasconcelos Fruet Jan 20 '22 at 01:18
  • What operating system? Console output depends on the terminal used and what encodings it supports. The encoding of the text file matters, too. If Linux, using UTF-8 for everything should work. For Windows, use wide characters and `_setmode` as in [this answer](https://stackoverflow.com/a/47501816/235698) – Mark Tolonen Jan 20 '22 at 01:40
  • It's a problem. Once the function ends, the memory used by `tabu` is freed up to be reused. Any destructors would run and the instances would be destroyed. The memory may still exist and be accessible to the program so you may see it lingering as a ghost, but at any time that memory could be reused by another object and be changed. Or, god forbid, you change the memory yourself and blow the crap out of the other object. You can't return a raw array because they're always returned as a pointer and the array the pointer references immediately dies. – user4581301 Jan 20 '22 at 01:58

3 Answers3

1

Just like every other possible kind of data that lives in your computer, it must be represented by a sequence of bytes. Each byte can have just 256 possible values.

All the carbon-based life forms, that live on the third planet from the sun, use all sorts of different alphabets with all sorts of characters, whose total number is much, more than 256.

A single byte by itself cannot, therefore, express all characters. The most simple way of handling all possible permutations of characters is to pick just 256 (or less) of them at a time, and assign the possible (up to 256) to a small set of characters, and call it your "character set".

Such is, apparently, your "tabuleiro.txt" file: its contents must be using some particular character set which includes the characters you expect to see there.

Your screen display, however, uses a different character set, hence the same values show different characters.

However, it's probably more complicated than that: modern operating system and modern terminals employ multi-byte character sequence, where a single character can be represented by specific sequences of more than just one byte. It's fairly likely that your terminal screen is based on multi-byte Unicode encoding.

In summary: you need to figure out two things:

  1. Which character set your file uses

  2. Which character set your terminal display uses

  3. Then write the code to properly translate one to the other

It goes without saying that noone else could possibly tell you which character set your file uses, and which character set your terminal display uses. That's something you'll need to figure out. And without knowing both, you can't do step 3.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
0

To print the Unicode characters, you can put the Unicode value with the prefix \u.

If the console does not support Unicode, then you cannot get the correct result.

Example:

#include <iostream>

int main() {
   std::cout << "Character: \u2563" << std::endl;
   std::cout << "Character: \u2551" << std::endl;
   std::cout << "Character: \u2560" << std::endl;
}

Output:

Character: ╣
Character: ║
Character: ╠
mvetois
  • 111
  • 11
-2

the answer is use the unsigned char in = manner like char than a = unicode num so this how to do it i did get an word like that when i was making an game engine for cmd so please up vote because it works in c++17 gnu gcc and in 2021 too to 2022 use anything in the place of a named a