How to copy data from .bin file to .pdf or .jpg in C++? -
i'm having problems reading binary data .bin file , writing pdf file. main goal receive file data on network, save data, files magic number , save , open file appropriate program. have data, save them binary file , right magic number. when try write binary data new pdf file, can't open resulting file.
i'm using ifstream/ofstream , opening files in binary mode. if .txt file, works , can open file in end , everything's fine (i know, there no magic number involved in .txt files...) opening broken pdf textedit shows first few lines of data written file should, after lines stops (but new pdf has same size original file!)
i hope understand problem , might have hints on problem is!
thank you!
roboneko
edit: here code problem has be. hope, helps...
long size; char * buffer; std::ifstream in(filenames[i].c_str(),std::ios::in | std::ios::binary); //filenames include filename in .bin format std::filebuf *pbuf; pbuf=in.rdbuf(); size=pbuf->pubseekoff (0,std::ios::end,std::ios::in); pbuf->pubseekpos (0,std::ios::in); char magic1[12] = {0}; in.read(magic1, sizeof(magic1)); unsigned char magic[12] = {0}; (int h = 0; h<sizeof(magic); h++) { magic[h] = (unsigned char) magic1[h]; } if ( magic[0] == 0x25 && magic[1] == 0x50 && magic[2] == 0x44 && magic[3] == 0x46 && magic[4] == 0x2d){ std::string temp = filenamesraw[i]; temp.append("test.pdf"); std::ofstream myfile( temp.c_str(), std::ios::out | std::ios::binary); buffer = new char [size]; in.seekg(0, std::ios_base::beg); pbuf->sgetn (buffer,size); in.close(); myfile.write(buffer,size); myfile.close(); delete[] buffer; std::string temp2 = "open "; temp2.append(temp); system(temp2.c_str()); }
Comments
Post a Comment