2

Basically I'm trying to write to a binary file so that when it is opened with a text editor it will display all ASCII characters. I noticed this works with notepad but doesn't work with notepad++ or open office and gives strange results. Why?

#include <fstream>
using namespace std;
int main () {
    ofstream file ("file.bin", ios::binary);
    for(int num = 0; num < 128; num++)
        file.write (reinterpret_cast<const char *>(&num), sizeof(num));
    file.close ();
    return 0;
}

So I expect the file when opened with a text editor to roughly reproduce this ASCII chart. When I open it with notepad I get this

   !   "   #   $   %   &   '   (   )   *   +   ,   -   .   /   0 

1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ] ^ _ ` a b c d e f g h i j k l m n o p q r s t u v w x y z { | } ~

When I open it with notepad++ I get this screen shot of notepad++

When I open it with OpenOffice.org Writer I select the default option to open with "Western Europe (Windows 1252/WinLatin 1)" and get a bunch of ###. Does this have to do with the byte-order-marker?

I tried modifying the program to use file.write (reinterpret_cast<const char *>(&num), sizeof(char)); since the int is being type casted to a char but then the program crashes.

Out of curiosity anyone got an explanation as to why OpenOffice writer comes up with # and spaces?

4

1 回答 1

5
for(int num = 0; num < 128; num++)
    file.write (reinterpret_cast<const char *>(&num), sizeof(num));

在这些行中,您num以二进制形式写入数字,即 4 字节整数。( sizeof(num), 哪里numint)。由于您写入的所有值都小于 128,因此前三个字节num总是0x000000. 因此,对于您编写的每个值,您将获得三个空值,然后是您想要的 ASCII 字符。

Microsoft 记事本绝对是愚蠢的,当它遇到不可打印的 ASCII 字符时,例如 NULL,它只会显示一个空格。注意你的 ASCII 值之间是如何有“空格”的。另外,我敢打赌它看起来更像这样,我用下划线替换了一些空格。请注意,第 10 个值(新行)会导致新行。

_________
__            !   "   #   $   %   &   '   (   )   *   +   ,   -   .   /   0   1   2   3   4   5   6   7   8   9   :   ;   <   =   >   ?   @   A   B   C   D   E   F   G   H   I   J   K   L   M   N   O   P   Q   R   S   T   U   V   W   X   Y   Z   [   \   ]   ^   _   `   a   b   c   d   e   f   g   h   i   j   k   l   m   n   o   p   q   r   s   t   u   v   w   x   y   z   {   |   }   ~   

Notepad++ 更加智能,它不会将所有奇怪的东西都转换为空格,而是向您显示它们的名称或编号。Notepad++ 本身似乎也对字符 13(回车)感到困惑,因为 13 通常也是新行的一部分。它(合理地)决定也将其设为一条新线。Notepad++ 对这个文件的处理可以说是更正确的。这可以通过查看前八个字符来验证:“ NUL NUL NUL NUL - SOH NUL NUL NUL”首先是零,然后是“SOH”字符。如果我们查看您的 ASCII 图表,它会显示第一个 ASCII 字符是“SOH - 标题开头”。

于 2013-07-23T22:20:10.907 回答