I wanted to read the file that is given in command line arguments and delete it after reading . This is what I'm doing.
char *filename = argv[1];
char *outputfile = strcat(argv[1], ".cmp");
fd = open(argv[1], O_RDONLY);
chars = read(fd, buf, BUFFERSIZE);
fd1 = creat(outputfile, 0644);
write(fd1, buf, BUFFERSIZE);
close(fd1);
close(fd);
unlink(argv[1]);
If I give "mytxt" in the command line, the code is supposed to create "mytxt.cmp" file and delete "mytxt", instead it is deleting "mytxt.cmp" keeping "mytxt" as it is. Why is it so? How can I delete a file given in command line arguments.