-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfileHelper.c
129 lines (115 loc) · 3.36 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
#include "fileHelper.h"
/////////////////////////////////////////////////////////////////
// writeStringToFile
// Aim: To write string to text file
// Input: file pointer, string to write
// Output: 1 if succeeded
/////////////////////////////////////////////////////////////////
int writeStringToFile(FILE* file, const char* str) {
if (!file) {
return 0;
}
fprintf(file, "%s\n", str);
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) {
if (!file) {
return 0;
}
size_t len = (size_t)strlen(str) + 1;
// 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: dynamic allocated string read from file
/////////////////////////////////////////////////////////////////
char* readStringFromTextFile(FILE* file, char* buffer, int size) {
// allocate dynamic memory for the string make sure to use cautiously!
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;
}
/////////////////////////////////////////////////////////////////
// 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!
if (!file) {
return NULL;
}
int len;
if (fread(&len, sizeof(int), 1, file) != 1) {
return NULL;
}
char* str = (char*)malloc((len + 1) * sizeof(char));
if (!str) {
return NULL;
}
if (fread(str, sizeof(char), len, file) != len) {
free(str);
return NULL;
}
str[len] = '\0';
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, void* fileType, size_t sizeOfElement) {
if (!file) {
return 0;
}
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) {
if (!file) {
return 0;
}
if (fread(readValue, sizeOfElement, 1, file) != 1) {
return 0;
}
return 1;
}