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
Can you explain what keyboard debouncing is, and where and why we us it? please give some examples
Explain which function in c can be used to append a string to another string?
What is unary operator?
Explain the use of 'auto' keyword in c programming?
What do you mean by a local block?
Why is not a pointer null after calling free? How unsafe is it to use (assign, compare) a pointer value after it is been freed?
Tell me is null always defined as 0(zero)?
What is the size of array float a(10)?
Write a progarm to find the length of string using switch case?
Why does notstrcat(string, "!");Work?
What are loops in c?
write a c program to calculate sum of digits till it reduces to a single digit using recursion
What is pointer in c?
C program to find all possible outcomes of a dice?
Explain what is a 'locale'?