c++ - How can writing memory to a filebuffer mutate it? -
c++ - How can writing memory to a filebuffer mutate it? -
for while now, have been experiencing extremely odd problem when trying write memory filebuffer in c++. problem occurs on mingw. when compile under gcc/linux, fine.
debugging session displaying problem
so basically, i'm writing code memory buffer filebuffer, , binary representation in file ends beingness different memory wrote. no, file not beingness modified @ later point, ensured using debugger exit programme after closing file. have no thought how possible, used valgrind see if there memory allocation problems, nope.
i'll paste of related code.
/// struct holding info info file class resourcefile { public: string name; uint32 size; char* data; resourcefile(string name, uint32 size); }; resourcefile::resourcefile(string name, uint32 size) : name(name), size(size) { // free'd in resourcebuilder's destruction info = (char*) malloc(size * sizeof(char)); } /// build info resource set of files class resourcebuilder { public: ofstream out; ///< file set resource vector<resourcefile> input; ///< list of input strings /// add together file disk resource void add_file(string filename); /// create file resource written void create_file(string filename); ~resourcebuilder(); }; void resourcebuilder::create_file(string filename) { // open specified file output out.open(filename.c_str()); uint16 number_files = htons(input.size()); out.write((char*) &number_files, sizeof(uint16)); foreach(vector<resourcefile>,input,i) { resourcefile& df = *i; uint16 name_size = i->name.size(); uint16 name_size_network = htons(name_size); out.write((char*) &name_size_network, sizeof(uint16)); out.write(i->name.c_str(),name_size); uint32 size_network = htonl(i->size); out.write((char*) &size_network, sizeof(i->size) ); out.write(i->data, i->size); } out.close(); /// \todo write crc }
the next how memory allocated in first place. possible source of error, because copypasted somewhere else without bothering understand in detail, don't know how method in allocated memory reason filebuffer output beingness different memory i'm writing.
void resourcebuilder::add_file(string filename) { // loads file , copies content memory // done resourcefile class , there // little problem this, namely memory // allocated in resourcefile directly, ifstream file; file.open(filename.c_str()); filebuf* pbuf=file.rdbuf(); int size=pbuf->pubseekoff (0,ios::end,ios::in); pbuf->pubseekpos (0,ios::in); resourcefile df(filename,size); pbuf->sgetn (df.data,size); file.close(); input.push_back(df); }
i'm out of ideas. it's not bug pertaining compiler setup, other people compiling code under mingw same error. explanation can think of @ point bug mingw's filebuffer library itself, have no idea.
you need open file in binary mode. when open in text mode on windows, line feeds (0x0a) converted cr/lf pairs (0x0d, 0x0a). on linux don't see because linux uses single lf (0x0a) line terminator in text files, no conversion done.
pass ios::binary
flag ostream::open
method:
out.open(filename.c_str(), ios::out | ios::binary);
you should utilize flag when reading binary files, opposite conversion isn't performed:
ifstream file; file.open(filename.c_str(), ios::in | ios::binary);
c++ c file memory
Comments
Post a Comment