forked from DingHe/unpv13e
-
Notifications
You must be signed in to change notification settings - Fork 2
/
wrapstdio.c
58 lines (46 loc) · 942 Bytes
/
wrapstdio.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
* Standard I/O wrapper functions.
*/
#include "unp.h"
void
Fclose(FILE *fp)
{
if (fclose(fp) != 0)
err_sys("fclose error");
}
FILE *
Fdopen(int fd, const char *type)
{
FILE *fp;
if ( (fp = fdopen(fd, type)) == NULL)
err_sys("fdopen error");
return(fp);
}
char *
Fgets(char *ptr, int n, FILE *stream)
{
char *rptr;
/*
* 当遇到文件结束符或错误时,fgets将返回一个空指针;
*
* Fgets包裹函数检查是否发生错误,若发生则中止进程,因此Fgets只是在遇到
* 文件结束符时才返回一个空指针
*/
if ( (rptr = fgets(ptr, n, stream)) == NULL && ferror(stream))
err_sys("fgets error");
return (rptr);
}
FILE *
Fopen(const char *filename, const char *mode)
{
FILE *fp;
if ( (fp = fopen(filename, mode)) == NULL)
err_sys("fopen error");
return(fp);
}
void
Fputs(const char *ptr, FILE *stream)
{
if (fputs(ptr, stream) == EOF)
err_sys("fputs error");
}