You need to check that state of your stream in the C++ code after creation, and after the read, to be sure that the operations were successful. Ie if (ifs) {...}
after creating the stream, and after reading it. If either operations failed, then after you are just reading uninitialised / undefined data from buf1
.
See http://www.cplusplus.com/reference/istream/istream/read/ for examples of using ifstream properly.