You're overwriting your data, memset() or fread() won't move the value of the "buffer" variable (how could they?), you have to do it by yourself.