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
How can you find the exact size of a data type in c?
Declare the structure which contains the following members and write in C list of all students who score more than 75 marks. Roll No, Name, Father Name, Age, City, Marks.
how to execute a program using if else condition and the output should enter number and the number is odd only...
write a c program to calculate sum of digits till it reduces to a single digit using recursion
What is const and volatile in c?
What is 02d in c?
Is fortran faster than c?
An application package has been provided to you without any documents for the following application. The application needs to be tested. How will you proceed?
#include
What is string concatenation in c?
How to define structures? ·
How do you define CONSTANT in C?
What is c definition?
Explain what is wrong with this program statement?
what is bit rate & baud rate? plz give wave forms