I do mini DB, and there was such a problem. The database works through structures, i.e. There are adding entries to the file.

void write(FILE *file, int readCount, Student *notes, int writeCount) { fseek(file, readCount * sizeof(Student), SEEK_SET); for(int z = 0; z < writeCount; z++){ fwrite(&notes[z], sizeof(Student), 1, file); } } 

And reading:

 Student *read(FILE *file, long int a) { fseek(file, 0, SEEK_SET); Student *readfile = new Student[a]; fread(readfile, sizeof(Student), a, file); return readfile; } 

Structure:

 struct Student { char firstname[30]; char secondname[30]; char lastname[30]; int age; int course; int group; }; 

OS: Windows

The problem is that when I want to add an entry in Russian, it is added, but when read, it displays the wrong encoding. How can I fix this?

  • Show the definition of the Student structure. - KoVadim
  • one
    And how do you observe (specifically) that the encoding is not the same? By the way, what OS (Windows, probably)? - avp
  • @avp, reading the structure from the file - Dmitry Alekseevich
  • one
    > I don’t know in which encoding the structure is written. There is no concept “Russian text”, there are only concepts “text encoded in CP1251”, “text encoded in KOI8-R”, “text encoded in CP866”, “text encoded in UTF-8”, etc. You can not write the Russian text in some incomprehensible encoding, and then work with it. - VladD
  • one
    We do not need a "compiled program". Need a definition of structure. But I will try to strain telepathy. Fields for which the encoding flies are declared as char * field; or std::string field; . With this approach, the above method of saving records will not work normally. And the flown encoding is still a small problem. Show the definition of the Student structure. Compile the code as much as I can, and the other participants. - KoVadim

1 answer 1

Perhaps, if conditions allow, it is worth using file streams. If it does not work automatically, you can force the encoding.