-
Notifications
You must be signed in to change notification settings - Fork 0
/
read.c
74 lines (64 loc) 路 1.45 KB
/
read.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <stdio.h>
#include <stdlib.h>
#include "get_input.h"
/* errors */
enum Errors {
NORMAL = 0,
USAGE = 1,
FILE_ERROR = 2,
CLOSE_ERROR = 3
};
/* pass a message to stderr */
void warn(const char* message) {
fprintf(stderr, "%s\n", message);
fflush(stderr);
}
/* pass a string to stdout */
void print(char* message) {
fprintf(stdout, "%s\n", message);
fflush(stdout);
}
/* post error to stderr */
int post_status(enum Errors error) {
const char* message = 0;
switch(error) {
case NORMAL:
message = "Program ended.";
break;
case USAGE:
message = "Usage: ./read.out file.";
break;
case FILE_ERROR:
message = "File could not be read.";
break;
case CLOSE_ERROR:
message = "File could not be closed.";
break;
default:
message = "Unknown error";
}
warn(message);
exit(error);
}
/* start reading a file */
int main(int argc, char** argv) {
if (argc != 2) {
post_status(USAGE);
}
/* open the file */
FILE* file;
if (!(file = fopen(argv[1], "r"))) {
post_status(FILE_ERROR);
}
/* read the line */
char* line = get_line(file);
/* close our file */
if (fclose(file) != 0) {
post_status(CLOSE_ERROR);
}
/* post the result */
print("Line was:");
print(line);
free(line);
post_status(NORMAL);
}