how do you redirect stdout value from a program to a file?
Answer Posted / ataraxic
int main(int argc, char *argv[], char *envp[])
{
char *p;
int fd = open("/tmp/mydata", O_CREAT|O_WRONLY);
if ( fd < 0 ) {
perror("open");
return -1;
}
/*
* close(2) system call deletes a descriptor from
* the per-process object reference table. In the
* per-process object reference table, stdin,
* stdout,stderr were placed at positions 0,1,2
* respectively.
*/
close(1);
/*
* Place our file descriptor at the place of stdout.
* Read man dup(2).
*/
dup(fd);
/*
* printf(3) is ultimately calling write(2) with
* first argument as 1
*/
printf("Hello there!\n");
p = getenv("MDEV");
if (p != NULL)
printf("MDEV is: %s\n", p);
p = getenv("SUBSYSTEM");
if (p != NULL)
printf("SUBSYSTEM is: %s\n", p);
return 0;
}
| Is This Answer Correct ? | 1 Yes | 0 No |
Post New Answer View All Answers
What is a char c?
Is it possible to initialize a variable at the time it was declared?
Explain which function in c can be used to append a string to another string?
What is structure padding in c?
Why c language is called c?
Mention four important string handling functions in c languages .
Why we not create function inside function.
What does 1f stand for?
How can I determine whether a machines byte order is big-endian or little-endian?
Is there a built-in function in C that can be used for sorting data?
write a program to find out prime number using sieve case?
Define VARIABLE?
How can I change their mode to binary?
Explain pointer. What are function pointers in C?
Why string is used in c?