It looks like it's actually the fwrite() line that's failing. You can tell from the assertion that your fopen() call failed. That means you don't have anything to write to but your still trying. That's why you get the assertion. Handle the case of when fopen() fails.