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 difference between far and near pointers?
What is structure in c language?
Explain the difference between #include "..." And #include <...> In c?
When we use void main and int main?
Combinations of fibanocci prime series
write a program to print the consecutive repeated character from the given string... input string is : hhhhjkutskkkkkggggj output should be like this: hhhhkkkkkgggg anyone help me...
Is it fine to write void main () or main () in c?
main() { inta=10,b=20; a>=5?b=100:b=200; printf("%d ",b); }
What is the difference between array and linked list in c?
What is the difference between mpi and openmp?
How can I find out the size of a file, prior to reading it in?
C program to find all possible outcomes of a dice?
What is the difference between class and object in c?
what is the format specifier for printing a pointer value?
Can two or more operators such as and be combined in a single line of program code?