Search code examples
c++file-iounicodefstreamansi

C++ create a file in Unicode instead of Ansi


Why C + +, create a Unicode file if you try to write a structure like this in the file? Part of the code:

struct stEjemplo
{
    char cadena1[9];
    char cadena2[9];
};

Write what I write in cadena1 and cadena2 shows me something like this in the file:

㈱㐳㘵㠷㠀㘷㐵㈳o

Example:

fstream file("File.dat");
if(!file.is_open())
{
    file.open("File.dat", ios::in | ios::out | ios::trunc);
}
stEjemplo somest = {0};
strcpy(somest.origen, "SomeText");
strcpy(somest.destino, "SomeText");
file.clear();
file.seekg(0,ios::beg); //ios::end if existing information
file.write(reinterpret_cast< char*>(&somest), sizeof(stEjemplo));
file.close();

Results this:

潓敭敔瑸匀浯呥硥t

Note the "t" in final (is the "t" in final of the second "SomeText")

But if my structure was:

struct stEjemplo
{
    int number; //then I assign 1324
    char cadena1[9];
    char cadena2[9];
};

Results: , SomeText SomeText or

struct stEjemplo
{
    bool x; //then I assign true o false
    char cadena1[9];
    char cadena2[9];
};

would result something like: SomeText SomeText

EDIT:

If the 00 (NULL character) in hex editor is set in odd position (starting at 0, for example: 1, 3, 5, 7, 9, etc etc) I have the problem, but if the 00 is set in a pair position and is not preceded by another 00, the problem is solved.


Solution

  • Known Notepad bug. Not your fault.