I'm trying to write a program to format a text file and I can't figure out how to write to the file passed in the command line.

Here is what I have tried in pseudocode:
-open the file argv[1] for reading
-open my temporary file for writing
-read a line of the original file into a buffer via "fgets"
-format it accordingly
-put the newly formatted line into the temp file via "fputs"
-do the previous 3 steps until the original file ends
-close both files via "fclose"
-remove the original file via "remove"
-rename the temp file to the original file via "rename"

This isn't working (Seg Fault) I think because it is not able to delete the file contained in the command line (argv[1]). I have tested, and the temporary file is getting the data successfully--the problem lies in the transferring of the contents of the temporary file to the original file (or at least renaming the temp file to the original). I tried other methods of transferring the data, but they all return a Seg Fault.

Am I going about this all wrong? Or is there a trick to manipulating files included in the command line?