-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathefile.c
118 lines (91 loc) · 2.19 KB
/
efile.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/**************************************/
/* efile.c */
/**************************************/
#include <stdio.h>
#include <stdlib.h>
#include "util.h"
/**************************************/
/* Main functions */
/**************************************/
FILE *mainFilePtr;
/**************************************/
/* Open file */
/**************************************/
FILE *eopen(char *fileName, char *fileMode)
{
mainFilePtr = fopen(fileName, fileMode);
return mainFilePtr;
}
FILE *eopenr(char *fileName)
{
mainFilePtr = fopen(fileName, "r");
return mainFilePtr;
}
FILE *eopenw(char *fileName)
{
mainFilePtr = fopen(fileName, "w");
return mainFilePtr;
}
FILE *eopena(char *fileName)
{
mainFilePtr = fopen(fileName, "a");
return mainFilePtr;
}
/**************************************/
/* Read from file */
/**************************************/
char *eread(FILE *filePtr)
{
if (filePtr == NULL)
filePtr = mainFilePtr;
return readTillDelimeter(EOF, filePtr, 0);
}
/**************************************/
/* Print to screen */
/**************************************/
void eprint(FILE *filePtr)
{
if (filePtr == NULL)
filePtr = mainFilePtr;
char printCharacter;
while (1)
{
printCharacter = fgetc(filePtr);
if (printCharacter == EOF)
break;
printf("%c", printCharacter);
}
printf("\n");
}
/**************************************/
/* Read a line */
/**************************************/
char *ereadLine(FILE *filePtr)
{
if (filePtr == NULL)
filePtr = mainFilePtr;
return readTillDelimeter('\n', filePtr, 0);
}
/**************************************/
/* Read custom number of lines */
/**************************************/
char *ereadCustomLines(FILE *filePtr, int lines)
{
if (filePtr == NULL)
filePtr = mainFilePtr;
return readTillDelimeter('\0', filePtr, lines);
}
/**************************************/
/* Write to file */
/**************************************/
int ewrite(FILE *filePtr, char *data)
{
if (filePtr == NULL)
filePtr = mainFilePtr;
for (int i = 0; *data != '\0'; i++)
fputc(*data++, filePtr);
return 1;
}
/**************************************/
/* EOF */
/**************************************/