Your fopen is inside your loop, meaning you are opening the file over and over...and probably over-writing everything in it