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


Please Help Members By Posting Answers For Below Questions

What is difference between far and near pointers?

604


What is structure in c language?

614


Explain the difference between #include "..." And #include <...> In c?

623


When we use void main and int main?

588


Combinations of fibanocci prime series

1108






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...

1481


Is it fine to write void main () or main () in c?

543


main() { inta=10,b=20; a>=5?b=100:b=200; printf("%d ",b); }

904


What is the difference between array and linked list in c?

593


What is the difference between mpi and openmp?

728


How can I find out the size of a file, prior to reading it in?

616


C program to find all possible outcomes of a dice?

1851


What is the difference between class and object in c?

578


what is the format specifier for printing a pointer value?

609


Can two or more operators such as and be combined in a single line of program code?

801