c++ - When I write a file in mode "wb", what will happens? -
when write "asklfj123" file in mode "wb", open in vim, why can still see asklfj ?but can not see 123 in vim. mean when write in mode "wb", program not convert ascii code, editor open ascii code, should garbage values in text editor, isn't it? why can still see them?
binary mode means "don't translate anything".
in "text" mode (that "not binary mode"), on systems, representation of example newlines different between os's file representation , standard '\n'
c uses newline representation.
in systems there special characters represent end of file. when in text mode, these characters translated , understood mean newline , end of file, respectively.
now, writing text in "binary" mode work fine, , suspect problem else - without code, it's impossible answer what. mention of vim
makes me think using linux or other unix-based system, newline indeed same '\n'
, no translation done in these systems.
edit:
given code, it's typical "undefined behaviour", because:
char test[30] = "adadsf123"; ... fwrite(test, sizeof(double), 6, fp);
the arguments fwrite
data written, size of each member, , number of items, , file object write to.
in case, string of 30 bytes given input, sizeof(double) size of each item, , 6 items. since sizeof(double)
more 30, random garbage being output. random garbage is, depending on stored memory after test
variable. in code posted, appears fp
variable. it's hard fp
's value be. fwrite
doing has been told, writing binary data binary file. ascii characters, in "binary" still appear same, binary code of 'a' 01000001, , appear, if interpreted ascii shows 'a' - , vim , putc
interpret ascii.
Comments
Post a Comment