Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
script3r
GitHub Repository: script3r/os161
Path: blob/master/user/testbin/ft3/ft3.c
734 views
1
#include <unistd.h>
2
#include <stdio.h>
3
#include <stdlib.h>
4
5
#define BUF_SIZE 128
6
7
int main( int argc, char *argv[] ) {
8
int fd_out;
9
int err;
10
11
(void)argc;
12
(void)argv;
13
14
//try to open a sample output file.
15
fd_out = open( "/hello.txt", O_WRONLY );
16
if( fd_out < 0 ) {
17
printf( "open: error opening the file." );
18
return -1;
19
}
20
21
//close stdout.
22
err = close( STDOUT_FILENO );
23
if( err ) {
24
printf( "close: could not close stdout." );
25
return -1;
26
}
27
28
//route stdout to fd_out.
29
dup2( fd_out, STDOUT_FILENO );
30
31
//print a sample message to STDOUT.
32
//this should end up inside the file.
33
printf( "I should be seeing this message inside hello.txt\n" );
34
35
//close fd_out
36
err = close( fd_out );
37
if( err )
38
return err;
39
40
//stdout will be closed by exit().
41
return 0;
42
}
43
44