-2

I made a program as below

#include<iostream.h>
#include<string.h>
#include<stdio.h>
#include<fstream.h>
    void main() {
        char name[24];
        cout << "enter string :";
        gets(name);
        ofstream fout;
        fout.open("bin_data",ios::out|ios::binary);
        fout.write((char*)&name,10);
        fout.close();
    }

But when I open the file bin_data by notepad I find that the string is saved in text format not in binary form...... Please help...

This code can save a word of 10 char. But when I compile this code by turbo c++ v4.5 I find that. When I input 1 or 2 letter word it saves in text format(ignore garbage value) but when I input a word of 3 to 7 letter long it saves in binary format. and in 9 and 10 letter word again in text format..... Can anyone tell me the reason...? Please compile and run program as I mentioned above and answer

Atheerth
  • 5
  • 5
  • 2
    You have a rather severe misunderstanding of 'binary' and 'text'. Writing text to a file in 'binary mode' writes text. – user5151179 Nov 10 '15 at 07:54
  • Please read many things about proper serialization, charsets, and Unicode. That are huge topics, don't think you're done in in day or something like this. – deviantfan Nov 10 '15 at 12:32

1 Answers1

2

Your data only contains text. It is represented by the very same bits in both text format and binary format.

Binary format means that your data is written to the file unchanged. If you were to use text format, some non-text characters would be modified. For example, byte 10 (which represents newline) could be changed to operating system specific newline (two bytes, 15 and 10, on Windows).

For binary values of text characters, see http://www.asciitable.com/

Your second example has a buffer overflow.

char name[24];
fout.write((char*)&name,10);

You reserve 24 bytes of data, which is filled by random bytes that happen to be at that point of memory. When you save a 2-character string to the buffer, it only overwrites first three bytes. The third byte is set to value 0, which tells you that the text ends at that point. If you were to call strlen(), it would tell you the amount of characters before the first 0 byte.

If your input is a 2-character text, and you choose to write 10 bytes from your buffer, the 7 bytes in the end are filled with invalid data. Note that this does not cause an access violation, because you have reserved data for 24 bytes.

See also: https://en.wikipedia.org/wiki/Null-terminated_string

VLL
  • 9,634
  • 1
  • 29
  • 54
  • @AtheerthNalpadi I edited this to answer your second question. – VLL Nov 10 '15 at 10:00
  • When using without strlen() it atleast saves data in binary format of word length 3 to 7 characters – Atheerth Nov 10 '15 at 10:11
  • You are not seeing binary data in the file, because text characters are represented by the same bytes in binary format and text format. This means your text editor will open the file without complaining about binary characters. – VLL Nov 10 '15 at 10:24
  • plz compile and run the program and enter 2char word first and open bin_data file and u see it iis in text format (with some garbage values ignore it) and run program again and type a 4 or 5 letter word and again open the bin_data file again and u will see in binary form as – Atheerth Nov 10 '15 at 16:56
  • Binary file as 是哥哥繁华过分 – Atheerth Nov 10 '15 at 16:57