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
What are the general description for loop statement and available loop types in c?
What is array of structure in c?
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
What is the difference between a string copy (strcpy) and a memory copy (memcpy)? When should each be used?
How can I invoke another program or command and trap its output?
Explain how can I right-justify a string?
What is the time and space complexities of merge sort and when is it preferred over quick sort?
What is #include stdlib h?
Difference between Shallow copy and Deep copy?
Can you write the algorithm for Queue?
Is c++ based on c?
Combinations of fibanocci prime series
What is the difference between far and near in c?
How reliable are floating-point comparisons?
Can you write a programmer for FACTORIAL using recursion?