d.c
| 1 | #include <stdio.h> |
|---|---|
| 2 | |
| 3 | int
|
| 4 | main(void)
|
| 5 | {
|
| 6 | FILE *f = fopen("duptmp", "w"); |
| 7 | FILE *f1, *f2; |
| 8 | char line[100]; |
| 9 | |
| 10 | if (f == NULL) |
| 11 | return (1); |
| 12 | |
| 13 | fputs("1\n2\n3\n", f);
|
| 14 | fclose(f); |
| 15 | |
| 16 | f1 = fopen("duptmp", "r"); |
| 17 | if (f1 == NULL) |
| 18 | return (1); |
| 19 | f2 = fdopen(dup(fileno(f1)), "r");
|
| 20 | if (f2 == NULL) |
| 21 | return (2); |
| 22 | fgets(line, sizeof(line), f1);
|
| 23 | printf("read1: %s", line);
|
| 24 | fgets(line, sizeof(line), f2);
|
| 25 | printf("read2: %s", line);
|
| 26 | return (0); |
| 27 | } |