forked from Danielbet21/Amusement_Park
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileHelper.c
193 lines (154 loc) · 4.9 KB
/
fileHelper.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include "fileHelper.h"
/////////////////////////////////////////////////////////////////
// writeStringToFile
// Aim: To write string to text file
// Input: file pointer, string to write
// Output: 1 if succeeded
/////////////////////////////////////////////////////////////////
int writeStringToTextFile(FILE* file, const char* str) {
IS_FILE_NULL(file);
fprintf(file, "%s\n", str);
return 1;
}
/////////////////////////////////////////////////////////////////
// writeIntToTextFile
// Aim: To write int to text file
// Input: file pointer, int to write
// Output: 1 if succeeded
/////////////////////////////////////////////////////////////////
int writeIntToTextFile(FILE* file, int num) {
IS_FILE_NULL(file);
fprintf(file, "%d\n", num);
return 1;
}
/////////////////////////////////////////////////////////////////
// WriteDoubleToTextFile
// Aim: To write double to text file
// Input: file pointer, double to write
// Output: 1 if succeeded
/////////////////////////////////////////////////////////////////
int writeDoubleToTextFile(FILE* file, const double num) {
IS_FILE_NULL(file);
if (fprintf(file, "%f\n", num) < 1) {
return 0;
}
return 1;
}
/////////////////////////////////////////////////////////////////
// readDoubleFromTextFile
// Aim: To read double from text file
// Input: file pointer
// Output: double read from file
/////////////////////////////////////////////////////////////////
int readDoubleFromTextFile(FILE* file, double* num) {
IS_FILE_NULL(file);
if (fscanf(file, "%lf", num) < 1) {
return 0;
}
return 1;
}
/////////////////////////////////////////////////////////////////
// writeStringTobinFile
// Aim: To write string to binary file
// Input: file pointer, string to write
// Output: 1 if succeeded
/////////////////////////////////////////////////////////////////
int writeStringTobinFile(FILE* file, const char* str) {
IS_FILE_NULL(file);
size_t len = (size_t)strlen(str) + 1; // +1 for the null terminator
// write the length of the string
if (fwrite(&len, sizeof(int), 1, file) != 1)
return 0;
// write the string
if (fwrite(str, sizeof(char), len, file) != len)
return 0;
return 1;
}
/////////////////////////////////////////////////////////////////
// readStringFromTextFile
// Aim: To read string from text file
// Input: file pointer
// Output: string read from file loaded in buffer
/////////////////////////////////////////////////////////////////
char* readStringFromTextFile(FILE* file, char* buffer, int size) {
char* ok;
if (buffer != NULL && size > 0)
{
do { //skip only '\n' strings
ok = fgets(buffer, size, file);
} while (ok && ((strlen(buffer) <= 1) && (isspace(buffer[0]))));
if (ok)
{
char* back = buffer + strlen(buffer);
//trim end spaces
while ((buffer < back) && (isspace(*--back)));
*(back + 1) = '\0';
return buffer;
}
buffer[0] = '\0';
}
return NULL;
}
/////////////////////////////////////////////////////////////////
// readIntFromTextFile
// Aim: To read int from text file
// Input: file pointer
// Output: int read from file
/////////////////////////////////////////////////////////////////
int readIntFromTextFile(FILE* file, int* num) {
IS_FILE_NULL(file);
if (fscanf(file, "%d", num) != 1) {
return 0;
}
return 1;
}
/////////////////////////////////////////////////////////////////
// readStringFromBinFile
// Aim: To read string from binary file
// Input: file pointer
// Output: dynamic allocated string read from file
/////////////////////////////////////////////////////////////////
char* readStringFromBinFile(FILE* file) {
// allocate dynamic memory for the string make sure to use cautiously!
IS_FILE_NULL(file);
int len;
if (fread(&len, sizeof(int), 1, file) != 1) {
return NULL;
}
char* str = (char*)malloc((len) * sizeof(char)); // TODO it was len+1 before need to make sure it work
if (!str) {
return NULL;
}
if (fread(str, sizeof(char), len, file) != len) {
free(str);
return NULL;
}
//str[len] = '\0'; // TODO need to check if it is needed
return str;
}
/////////////////////////////////////////////////////////////////
// writeGeneralToBinFile
// Aim: To write any type of data to binary file
// Input: file pointer, data to write, size of the data
// Output: 1 if succeeded
/////////////////////////////////////////////////////////////////
int writeGeneralToBinFile(FILE* file, const void* fileType, size_t sizeOfElement) {
IS_FILE_NULL(file);
if (fwrite(fileType, sizeOfElement, 1, file) != 1) {
return 0;
}
return 1;
}
/////////////////////////////////////////////////////////////////
// readGeneralFromBinFile
// Aim: To read any type of data from binary file
// Input: file pointer, data to read, size of the data
// Output: 1 if succeeded
/////////////////////////////////////////////////////////////////
int readGeneralFromBinFile(FILE* file, void* readValue, size_t sizeOfElement) {
IS_FILE_NULL(file);
if (fread(readValue, sizeOfElement, 1, file) != 1) {
return 0;
}
return 1;
}