Hi developers, i try to write a file in binary format. But that file is not in binary format, i dont know what's wrong in my code

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    fstream file("file path", ios_base::binary | ios_base::in);
    char buf[] = "BinaryFile";
    file.write(buf, sizeof(buf));
    file.close();
}
//My file is not in binary format whats wrong in my code explain me guys...

Thanks in Advance

First of all, your file is in binary, because all data is binary on a computer.

Second, you use the sizeof operator incorrectly. sizeof gives you the size of the type at compile-time. In this case the type of buf is a pointer to char which, most likely, has either 4 or 8 bytes. So sizeof(buf) will equal either 4 or 8. So what you will get in the file output is either "Bina" or "BinaryFi".

So, to get the actual length of the string pointed to by buf, use the function strlen(buf).

Now, to the meat of it. What is "BinaryFile"?

In ascii characters notation (null-terminated string):
'B' 'i' 'n' 'a' 'r' 'y' 'F' 'i' 'l' 'e' '\0'
In decimal numbers notation:
 66 105 110  97 114 121  70 105 108 101   0
In hexadecimal numbers notation:
 42  69  6E  61  72  79  46  69  6C  65  00
In binary numbers:
 0100 0010 0110 1001 0110 1110 0110 0001 0111 0010 0111 1001 0100 0110 0110 1001 0110 1100 0110 0101 0000 0000

Now, the point is this: All of the above are exactly the same! A text editor, like notepad, loads the binary data (i.e. computer memory) and show the ascii character representation. While so-called hex editors, will load the binary data and show the hexadecimal number representation.

Whether you have intelligible text (like in your example) or raw data (like saving an integer or float, or any thing besides a string), they are both binary. Now, what people call an ascii file and a binary file is only a mere distinction between files that are human-readable because they contain the information as a chain of printable and understandable characters (thus opening them in notepad looks like it makes sense), and files that are computer-readable because they contain the information exactly the same way a program stores it (and obviously it is easier to process that information). So, in general, opening a binary file in notepad will print out a bunch of senseless characters because the ascii translation of those values make no sense. But, if you save a string, then the ascii translation will make sense and it will appear as though it is not binary (in the common perceived sense that it looks all weird in a text editor). It is still in binary format.

Try writing anything but a char array with the exact same code. Like an int for example:

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    fstream file("file path", ios_base::binary | ios_base::in);
    int buf = 16;
    file.write((char*)&buf, sizeof(buf));
    file.close();
}

sizeof gives you the size of the type at compile-time. In this case the type of buf is a pointer to char which, most likely, has either 4 or 8 bytes. So sizeof(buf) will equal either 4 or 8.

Not necessarily true. What sizeof() returns for an array is dependant on the type and size of the array and the currently-active scope of the program. If you are within the original scope of the whole array, it will give you the total length of the array in bytes. If the whole array is not in-scope, but you have a pointer to it, then you will get the size of the pointer, not the array. Either way, you still need to be cautious about using sizeof() on an array.


Run a debugger on this and watch "buffSize". It should be 11 (10 chars plus a NULL).

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    fstream file("file path", ios_base::binary | ios_base::in);
    char buf[] = "BinaryFile";
    int buffSize = sizeof(buf);
    file.write(buf, buffSize);
    file.close();
}
//My file is not in binary format whats wrong in my code explain me guys...

This should return 20 on most 32-bit compilers (5 x 4-byte integers = 20 bytes):

int intArray[5] = {0};
int arySize = sizeof(intArray);

@OP
You really would be better off doing something like this instead:

file.write((char*)&buf, strlen(buf));
commented: learn something new.Thanks!Fboy whatever he he! +2
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.