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
Why isn't it being handled properly?
Write a factorial program using C.
Is c language still used?
How many levels deep can include files be nested?
Where local variables are stored in c?
How can I make sure that my program is the only one accessing a file?
Which header file is used for clrscr?
"%u" unsigned integer print the a) address of variable b) value of variable c) name of a variable d) none of the above
what is event driven software and what is procedural driven software?
What is string constants?
How can I write data files which can be read on other machines with different word size, byte order, or floating point formats?
#define MAX(x,y) (x) >(y)?(x):(y) main() { inti=10,j=5,k=0; k= MAX(i++,++j); printf("%d..%d..%d",i,j,k); }
How are strings stored in c?
Is it possible to have a function as a parameter in another function?
What is the usage of the pointer in c?