Ticket #299: freopen.c

File freopen.c, 1.5 KB (added by KO Myung-Hun, 10 years ago)

Test case for freopen()

Line 
1#include <stdio.h>
2#include <errno.h>
3#include <io.h>
4#include <fcntl.h>
5
6int main( void )
7{
8    char buffer[ 512 ];
9
10    FILE *fp;
11
12    fp = fopen("freopen.c", "rt");
13    printf("original fp = %p\n", fp );
14    printf("freopen() = %p\n", freopen( NULL, "rb", fp ));
15    perror("freopen()");
16    printf("new fp = %p\n", fp );
17
18    printf("fcntl() = %x, O_BINARY = %x\n",
19           fcntl( fileno( fp ), F_GETFL ), O_BINARY );
20    printf("-----\n");
21
22    getchar();
23
24    printf("original stdin = %p\n", stdin );
25    printf("freopen() = %p\n", freopen( NULL, "rb", stdin ));
26    perror("freopen()");
27    printf("new stdin = %p\n", stdin);
28
29    printf("fcntl() = %x, O_BINARY = %x\n",
30           fcntl( fileno( stdin ), F_GETFL ), O_BINARY );
31    printf("-----\n");
32
33    printf("original stdout = %p\n", stdout );
34    printf("freopen() = %p\n", freopen( NULL, "wb", stdout ));
35    perror("freopen()");
36    printf("new stdout = %p\n", stdout);
37
38    printf("fcntl() = %x, O_BINARY = %x\n",
39           fcntl( fileno( stdout ), F_GETFL ), O_BINARY );
40    printf("-----\n");
41
42    printf("original stderr = %p\n", stderr );
43    printf("freopen() = %p\n", freopen( NULL, "wb", stderr ));
44    perror("freopen()");
45    printf("new stderr = %p\n", stderr);
46
47    printf("fcntl() = %x, O_BINARY = %x\n",
48           fcntl( fileno( stderr ), F_GETFL ), O_BINARY );
49    printf("-----\n");
50
51    printf("buffer = %s\n", fgets( buffer, sizeof( buffer ), stdin ));
52
53    fclose( fp );
54
55    return 0;
56}