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
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?