Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...


how do you redirect stdout value from a program to a file?



how do you redirect stdout value from a program to a file?..

Answer / 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

More C Interview Questions

can u give me the good and very optimised code for a car racing game?

0 Answers  


write a program to generate address labels using structures?

0 Answers   SJC,


What is pointer in c?

0 Answers  


in iso what are the common technological language?

0 Answers  


What is a pointer variable in c language?

0 Answers  


what is difference between declaring the pointer as int and char in c language?

3 Answers  


Design a program using an array that lists even numbers and odd numbers separately from the 12 numbers supplied by a user.

8 Answers  


main() { int i,j,A; for(A=-1;A<=1;A++) prinf("%d\t",!!A); }

6 Answers  


explain memory layout of a C program

2 Answers  


What is the right way to use errno?

0 Answers  


What is an auto variable in c?

0 Answers  


Is sizeof a keyword in c?

0 Answers  


Categories