-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxstdio.c
60 lines (50 loc) · 1.22 KB
/
xstdio.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
59
60
#include <stdio.h>
#include <stdlib.h>
FILE *xfopen(const char *path, const char *mode)
{
FILE *fp = fopen(path, mode);
if (!fp) {
fputs("unable to open file '", stderr);
fputs(path, stderr);
fputs("'\n", stderr);
exit(EXIT_FAILURE);
}
return fp;
}
void xfread(void *ptr, size_t size, size_t nmemb, FILE *stream)
{
if (nmemb != fread(ptr, size, nmemb, stream)) {
fputs("read error\n", stderr);
exit(EXIT_FAILURE);
}
}
void xfseek(FILE *stream, long offset, int whence)
{
if (0 != fseek(stream, offset, whence)) {
fputs("fseek error\n", stderr);
exit(EXIT_FAILURE);
}
}
void xfwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
{
if (nmemb != fwrite(ptr, size, nmemb, stream)) {
fputs("write error\n", stderr);
exit(EXIT_FAILURE);
}
}
int xfgetc(FILE *stream)
{
int c = fgetc(stream);
if (c == EOF) {
fputs("fgetc error\n", stderr);
exit(EXIT_FAILURE);
}
return c;
}
void xfputc(int c, FILE *stream)
{
if (EOF == fputc(c, stream)) {
fputs("fputc error\n", stderr);
exit(EXIT_FAILURE);
}
}