-
Notifications
You must be signed in to change notification settings - Fork 0
/
io.c
57 lines (48 loc) · 1.07 KB
/
io.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
#include <stdio.h>
#include <stdlib.h>
#include "io.h"
char *getNextLine(FILE *file) {
char *line = NULL;
size_t len = 0;
ssize_t read;
read = getline(&line, &len, file);
if (read == -1) {
return NULL;
}
return line;
}
void writeLabel(FILE *file, char *label) {
fprintf(file, "%s:\t", label);
}
void writeLine(FILE *file, char *line) {
fprintf(file, "%s\n", line);
}
void writeLineWithoutBreak(FILE *file, char *line) {
fprintf(file, "%s", line);
}
FILE *createFile(char *path) {
FILE *file = fopen(path, "w");
if (file == NULL) {
printf("Error: Could not create file %s\n", path);
exit(1);
}
return file;
}
FILE *getFile(char *path, char *mode) {
FILE *file = fopen(path, mode);
if (file == NULL) {
printf("Error: Could not open file %s\n", path);
exit(1);
}
return file;
}
void deleteFile(char *path) {
int status = remove(path);
if (status != 0) {
printf("Error: Could not delete file %s\n", path);
exit(1);
}
}
void closeFile(FILE *file) {
fclose(file);
}