I have this code that writes successfully a file:
ofstream outfile (path); outfile.write(buffer,size); outfile.flush(); outfile.close();
If you are using linux, try execl(), with the command mv.