Generally speaking (there are a few exceptions), you can't edit an existing file, you need to create a modified copy of it, and then replace the original file with the modified copy. (As for the exceptions, usually they still do a modified copy of the original file and do the necessary housecleaning behind the scenes.)

For problem 2, you will have to provide your code, there is no way we can help you with so little information.