-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileHandler.c
71 lines (52 loc) · 1.55 KB
/
fileHandler.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
#include<stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <stdbool.h>
#include "./fileHandler.h"
void readFile(char* filePath, char** str, int* size){
FILE* file = fopen(filePath, "r");
if(file == NULL){
fprintf(stderr, "Error while opening file: %s\n", filePath);
str = NULL;
*size = -1;
}
fseek(file, 0, SEEK_END);
int input_file_size = ftell(file);
rewind(file);
char *tempStr = (char*) malloc((input_file_size)* sizeof(char));
*size = input_file_size;
tempStr[input_file_size] = '\0';
char ch = fgetc(file);
int counter = 0;
while(ch != EOF){
tempStr[counter++] = ch;
ch = fgetc(file);
}
*str = tempStr;
fclose(file);
}
int writeFile(char* textContent, char* fileOutputPath){
FILE* file = fopen(fileOutputPath, "w");
if(file == NULL){
fprintf(stderr, "Error while proccessing file: %s\n", fileOutputPath);
return 0;
}
for(int i=0; i<strlen(textContent); i++){
fprintf(file, "%c", textContent[i]);
}
fclose(file);
return 1;
}
int appendToFile(char* textContent, char* fileOutputPath){
FILE* file = fopen(fileOutputPath, "a");
if(file == NULL){
fprintf(stderr, "Error while proccessing file: %s\n", fileOutputPath);
return 0;
}
for(int i=0; i<strlen(textContent); i++){
fprintf(file, "%c", textContent[i]);
}
fclose(file);
return 1;
}