为什么二进制文件中的某些数据显示为原样,而其他数据以奇怪的方式显示

时间:2022-09-15 21:58:25

I have code, which writes vector of such structures to a binary file:

我有代码,它将这种结构的向量写入二进制文件:

struct reader{
             char name[50];
             int card_num;
             char title[100];
             }

Everything works actually fine but when I, for example, write to file structure {One,1,One} and open .txt file, where it is stored, I see this:

一切都工作得很好但是当我,例如,写入文件结构{一,一,一}和打开.txt文件,它存储,我看到:

One ММММММММММММММММММММММММММММММММММММММММММММММММ One ММММММММММММММММММММММММММММММММММММММММММММММММММММММММММММММММММММММММММММММММММММММММММММММММ

一个ММММММММММММММММММММММММММММММММММММММММММММММММ一个ММММММММММММММММММММММММММММММММММММММММММММММММММММММММММММММММММММММММММММММММММММММММММММММММ

So I was asked why is it displayed so, what it depends on, but I could'nt give a good answer to that question

所以我被问到为什么它显示如此,它取决于什么,但我无法给出一个很好的答案

EDITED: Added code which I use to write to file

编辑:添加了我用来写入文件的代码

void Write_to_File(vector<reader>& vec){
    cin.clear();   // clearing
    fflush(stdin);// input stream
    const char* pointer = reinterpret_cast<const char*>(&vec[0]);
    size_t bytes = vec.size() * sizeof(vec[0]);

    fstream f("D:\\temp.txt", ios::out);
    f.close();

    ofstream file("D:\\temp.txt", ios::in | ios::binary);
    file.write(pointer, bytes);
    file.close();

    remove("D:\\lab.txt");
    rename("D:\\temp.txt", "D:\\lab.txt");
    cout << "\n*** Successfully written data ***\n\n";
}

P.S. When I read from file everything is ok

附:当我从文件中读取一切都没问题

2 个解决方案

#1


0  

You write 154 octets in a file, only One and One are char, so your text editor try to read char but get mostly garbage. You write binary, you should not expect to have something readable.

你在一个文件中写了154个八位字节,只有一个和一个是char,所以你的文本编辑器尝试读取char但大部分都是垃圾。你写二进制文件,你不应该期望有可读性。

#2


0  

Why some data in binary file is shown as it is and other is shown in a strange way

为什么二进制文件中的某些数据显示为原样,而其他数据以奇怪的方式显示

It seems that you are trying to read the binary data as if it contained character encoded data. Some of it does - but not all. Perhaps this is why you think that it seems strange. Other than that, the output seems perfectly reasonable.

您似乎正在尝试读取二进制数据,就好像它包含字符编码数据一样。其中一些 - 但不是全部。也许这就是为什么你认为这看起来很奇怪。除此之外,输出似乎非常合理。

why is it displayed so

为什么会这样显示出来

Because that is the textual representation of the data that the object contains in the character encoding that your reader uses.

因为这是对象在您的阅读器使用的字符编码中包含的数据的文本表示。

what it depends on

这取决于什么

It depends on the values that you have initialized the memory to have. For example the first character is displayed as O because you have initialized name[0] with the value 'O'. Some of the data is padding between members that can not be initialized directly. What the value of those bytes depends on is unspecified.

它取决于您初始化内存的值。例如,第一个字符显示为O,因为您已使用值“O”初始化名称[0]。一些数据是成员之间的填充,无法直接初始化。这些字节的值取决于未指定的内容。

#1


0  

You write 154 octets in a file, only One and One are char, so your text editor try to read char but get mostly garbage. You write binary, you should not expect to have something readable.

你在一个文件中写了154个八位字节,只有一个和一个是char,所以你的文本编辑器尝试读取char但大部分都是垃圾。你写二进制文件,你不应该期望有可读性。

#2


0  

Why some data in binary file is shown as it is and other is shown in a strange way

为什么二进制文件中的某些数据显示为原样,而其他数据以奇怪的方式显示

It seems that you are trying to read the binary data as if it contained character encoded data. Some of it does - but not all. Perhaps this is why you think that it seems strange. Other than that, the output seems perfectly reasonable.

您似乎正在尝试读取二进制数据,就好像它包含字符编码数据一样。其中一些 - 但不是全部。也许这就是为什么你认为这看起来很奇怪。除此之外,输出似乎非常合理。

why is it displayed so

为什么会这样显示出来

Because that is the textual representation of the data that the object contains in the character encoding that your reader uses.

因为这是对象在您的阅读器使用的字符编码中包含的数据的文本表示。

what it depends on

这取决于什么

It depends on the values that you have initialized the memory to have. For example the first character is displayed as O because you have initialized name[0] with the value 'O'. Some of the data is padding between members that can not be initialized directly. What the value of those bytes depends on is unspecified.

它取决于您初始化内存的值。例如,第一个字符显示为O,因为您已使用值“O”初始化名称[0]。一些数据是成员之间的填充,无法直接初始化。这些字节的值取决于未指定的内容。