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

Program to find larger of the two numbers without using if-else,while,for,switch

11 Answers   iNautix, Wipro,


what is the program to display your name in any color?

2 Answers   HCL,


What is the newline escape sequence?

0 Answers  


The file stdio.h, what does it contain?

0 Answers  


Explain what is page thrashing?

0 Answers  


Write a program that takes a 5 digit number and calculates 2 power that number and prints it.

5 Answers   TCS, Vimukti Technologies,


If input is 123 then how to print 100 and 20 and 3 seperately?

4 Answers  


Write a program of advanced Fibonacci series.

0 Answers   Aspiring Minds,


any string of bits of length 'n' represents a unique non- negative integer between.............?

2 Answers  


main() { char *p; p="Hello"; printf("%c\n",*&*p); }

2 Answers   ME,


What is extern variable in c with example?

0 Answers  


How can I open files mentioned on the command line, and parse option flags?

0 Answers  


Categories