. .
.
- . .
. () , .
int open (char *pathname, int oflag[, int pmode]).
, pathname, , oflag. oflag . pmode , , . Pmode . , open, fcntl.h. " ".
. -1 .
. . , (handle) . . , , . ³ .
handle
int close(int handle).
0, -1 - .
ѳ- 5 , ( 0), (1), (2), (3) (4):
. , .
.
#include <fcntl.h>
#include <types.h>
#include <stat.h>
#include <io.h>
main()
{
int e;
close(2);
if(-1==(e=open("file.err",O_WRONLY|O_CREAT|O_APPEND,S_IWRITE)))
rror(" ");
}
close , . file.err. . 2. , rror file.err.
|
|
dup dup2 , ' .
int dup(int handle)
, . handle, handle. .
int fh,q;
fh=open("data",O_WRONLY|O_CREAT|O_APPEND,S_IWRITE);
q=dup(fh);
"data" fh q.
int dup2(int handle1, int handle2)
handle1 handle2. (), . , handle1 handle2.
dup2 0.
#include <stdio.h>
#include <io.h>
#include <fcntl.h>
#include <stat.h>
main()
{int e;
e=open("file.out",O_WRONLY|O_CREAT|O_APPEND,S_IWRITE);
dup2(e,1);
printf("\n");
}
file.out. "".
, dup dup2 , . . , , . , , , .
-1, . errno :
EBADE - handle
EMFILE - handle ( ).
dup - dup2 , , .
, -, . ( 2.)
, 5 , ' . : , , , . , - - .
, .
- , : stdin ( ), stdout ( ), stderr (), stdaux ( ), stdprn ( ). , fputc('*', stdout) '*' .
|
|
stdin, stdout, stderr, stdaux stdprn , .
FILE *freopen(char *pathname,char *type, FILE *stream);
, ' stream, stream , pathname. , ' stream, type. " ". :
"r" - ( );
"w" - , , ;
"a" - (); , ;
NULL .
,
FILE * stream = freopen ("data2","",stdout);
data2, .
.
1. , . , .
2. , 䳿
) ³
open("data",O_WRONLY|O_CREAT|O_APPEND,S_IWRITE)
' N 4; dup dup2;
) , , , data;
) , ;
) ) ) 5 . 5 , .
3. , 䳿 :
) , ' , ;
) puts(char*), ;
) , ' "con";
) ), ) ) 5 . , 5 .
.
1. - ?
2. ?
3. ?
5. 䳿 ?
7. ?
8. ' ?
9. , - ?
10. dup dup2 ?
11. dup2 close dup?
12. dup ﳿ ?
13. ?
14. 䳿 freopen?
5.