It's not fwrite() which is adding stuff. It's because "magic_quotes_gpc" facility is enabled in your PHP.
The magic_quotes_gpc facility is good for beginners working with databases from PHP (it prevents some disasters from bad coding), but it can also be very annoying, which you have experienced.
You may turn off that facility (see the manual) or use stripslashes() on external variables which you want to use in your code.