[ back to toc ]

C programming: copying one file to another

Date: 2002/03/21 08:54

Q:
I'm using Unix, trying to do this networking assignment in C.

how do you create a file and copy it to another existing file?

A:
You open the source file using fopen("infile","rb") and the destination
file fopen("outfile","wb").

After the two successful opens you read the source file using getc and
write the destination file using putc until EOF comes.

Finally you close the files.

Well, this was a bit longer than writing the program, but this being a
class assignment (as it should be) I wanted to leave some way for to think
on the solution and to learn from practice.

(BTW: "r" and "w" are sufficient on UNIX, but more portable regarding
Windows if you use "rb" and "wb" and does not harm UNIX)

regards,
Peter

[ back to toc ]