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?

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 are the general description for loop statement and available loop types in c?

1079


What is array of structure in c?

1111


why arguments can generally be passed to functions a) sending the values of the arguments b) sending the addresses of the arguments c) a & b d) none of the above

1080


What is the difference between a string copy (strcpy) and a memory copy (memcpy)? When should each be used?

1069


How can I invoke another program or command and trap its output?

1058


Explain how can I right-justify a string?

1007


What is the time and space complexities of merge sort and when is it preferred over quick sort?

1021


What is #include stdlib h?

1065


Difference between Shallow copy and Deep copy?

1977


Can you write the algorithm for Queue?

2044


Is c++ based on c?

1032


Combinations of fibanocci prime series

1560


What is the difference between far and near in c?

1007


How reliable are floating-point comparisons?

1047


Can you write a programmer for FACTORIAL using recursion?

994