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 will be the output of" printf("%d%d",scanf("%d% d",&a&b));"

4 Answers  


Explain what is gets() function?

0 Answers  


What is the difference between int main and void main in c?

0 Answers  


struct node { int *a; char *b; char array[12]; }; struct node m,*n; assign the value in *a,*b,char array[12]

3 Answers  


What are multidimensional arrays?

0 Answers  






Define C in your own Language.

0 Answers   Motorola,


Write a program which has the following seven functions. The functions should be: • main() this calls the other 6 functions • fget_long() a function which returns a long data type from a file • fget_short() a function which returns a short integer variable from a file • fget_float() a function which returns a floating point variable from a file • fprt_long() a function which prints its single, long argument into a file • fprt_short() a function which prints its single, short argument into a file • fprt_float() a function which prints its single, floating point argument into a file. You should use fscanf() to get the values of the variables from the input (the file) and fprintf() to print the values to the other file. Pay attention to using the correct format for each of the data types.

0 Answers  


What is wild pointer in c?

0 Answers  


How are pointers declared in c?

0 Answers  


Using which language Test cases are added in .ptu file of RTRT unit testing???

0 Answers  


size maximum allocated by calloc()

3 Answers   DELL,


what is the different between data structure and data type?

1 Answers   Ignou,


Categories