if(fd=open(argv[1],O_WRONLY|O_CREAT|O_TRUNC,S_IRUSR|S_IWUSR|S_IROTH|S_IRGRP)<0)
Please mind operator precedence (<
trumps =
), use parentheses. You are setting fd
to 0 (stdin) on success.
write(STDOUT_FILENO,buf,nread);
This line fails because stdin is not open for writing.