c++ - using fread() to read into a char * buffer -



c++ - using fread() to read into a char * buffer -

i've been working on compression programme text files uses trie , custom bitstrings each character. file i'm reading written using fwrite(name, wb), , have looked on file (in binary mode) , have verified bits right should have been written. problem having reading binary file file can read.

void read2(string fname, ofstream &outf) { fname += ".mcp"; outf << endl; std::ifstream inf(fname, std::ios::binary); std::vector<char> data{ std::istreambuf_iterator<char>(inf), std::istreambuf_iterator<char>() }; std::cout << "size: " << data.size() << '\n'; std::copy(data.begin(), data.end(), std::ostreambuf_iterator<char>(outf)); }

the output function (cout) follows:

size: 25

(using smaller test file)

the ofstream output should read: 10001110 00000100 01001001 10110001 10011001 00101011 01101000 10010101 01000101 01010001 01111010 01101001 10100110 01000000 01111010 00010111 00000110 10101000 11100000 01000101 11011100 11110011 10010111 10001011 00000000 binary form of numbers read: (sorry reading hex code, copied straight output file)

c5 bd 04 49 c2 b1 e2 84 a2 2b 68 e2 80 a2 45 51 7a 69 c2 a6 40 7a 17 06 c2 a8 c3 a0 45 c3 9c c3 b3 e2 80 94 e2 80 b9 20

i used hex editor plugin notepad++ view bits.

the reply question simple, ofstream object, outf using output info not opened in binary mode, created new object in binary mode , output , worked.

void read2(string fname, ofstream &outf) { string fname1 =fname+ ".mcp", outfile="binarycheck"; std::ifstream inf(fname1, std::ios::binary); std::ofstream ouf("binarycheck.txt", std::ios::binary); std::vector<unsigned char> data{ std::istreambuf_iterator<char>(inf), std::istreambuf_iterator<char>() }; std::cout << "size: " << data.size() << '\n'; std::copy(data.begin(), data.end(), std::ostreambuf_iterator<char>(ouf)); }

c++ pointers char buffer fread

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -