I want to create a binary file. But when i input string "hello world!" for the file, the following code cann't create a binary file, it create a text(ASCII) file, why? Or the string must be converted to hex numbers for a binary file?
Development enviroment: Microsoft Visual Studio 2008.
#include <fstream>
#include <iostream>
using namespace std;
What exactly do you mean by a binary file?
Why do you think you are not creating a binary file?
From your description and code, it looks like you are creating a binary file which should be 12 bytes in size. If you opened your "output" file in wordpad, I would expect to see "hello world!".
The only differences between binary files and text files are that text files may perform special processing on end-of-line and end-of-file characters (classic behavior is to combine carraige returns and linefeeds into a single newline, or to treat control-Z as the end of file).
If you want to see something like "104 101 108 108 111 32 ...", where 104 is the decimal code for the letter 'h', you do not want a "binary file", you want to do a conversion. Try converting each character individual into a decimal value and print those values to the screen first. Once you get that done, then try writing them to a file.