slip11
By: Anonymous5/19/2023120 views Public Note
#include
#include
#include
int
main(int argc, char **argv)
{
int pid, status;
int newfd; /* new file descriptor */
if (argc != 2) {
printf("Wrong arguments");
exit(1);
}
if ((newfd = open(argv[1], O_CREAT|O_TRUNC|O_WRONLY, 0644)) < 0) {
printf("open failed"); /* open failed */
exit(1);
}
printf("This goes to the standard output.\n");
printf("Now the standard output will go to \"%s\".\n", argv[1]);
/* this new file will become the standard output */
/* standard output is file descriptor 1, so we use dup2 to */
/* to copy the new file descriptor onto file descriptor 1 */
/* dup2 will close the current standard output */
// dup2(newfd, 1);
close(1);
dup(newfd);
printf("This goes to the file output.txt.\n");
printf("This is test file");
exit(0);
}