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

What is the package for freshers(Non IIT) in amazon(hyderabad). And what is the same for those who are a contract employee.

0 Answers  


What does #pragma once mean?

0 Answers   Celstream,


Is a pointer a kind of array?

0 Answers  


What is struct node in c?

0 Answers  


Prove or disprove P!=NP.

5 Answers   Microsoft,






Is Exception handling possible in c language?

0 Answers   Wipro,


Can we use visual studio for c?

0 Answers  


What is a protocol in c?

0 Answers  


How to print "I Love My India" without using semi colon?

4 Answers  


char ch=10;printf("%d",ch);what is the output

14 Answers   Accenture,


What is the purpose of macro in C language?

0 Answers   Fidelity,


A float occupies 4 bytes in memory. How many bits are used to store exponent part? since we can have up to 38 number for exponent so 2 ki power 6 6, 6 bits will be used. If 6 bits are used why do not we have up to 64 numbers in exponent?

0 Answers  


Categories