I have written the following code to copy one file to another. Although the code works, the code still prints both error messages. Why is this ? I am a complete beginner to Unix and C programming(although I have worked with C++ before), so any help in as much detail as possible would be great. Thanks !
int main(int argc, char *argv[])
{
int n;
char buf[4096];
while ((n=read( open(argv[1], O_RDONLY) , buf, 4096))>0)
{
if (write(creat(argv[2], S_IREAD | S_IWRITE ), buf, n)!=n)
printf("Error writing to file.\n");
}
if (n<0)
printf("Error reading from file.\n");
exit(0);
}