-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_handling.c
More file actions
121 lines (115 loc) · 2.31 KB
/
Copy pathfile_handling.c
File metadata and controls
121 lines (115 loc) · 2.31 KB
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
#include "file_handling.h"
/*
* Function:
* getFile
*
* Description:
* Opens a file and creates the name for the output file;
*
* Arguments:
* Pointer to a file;
* Pointer to an array that contains the name of the file;
* Pointer to a helper array;
*
* Return value:
* Pointer to the file;
*
*/
FILE* getFile(FILE *fp, char* filename, char *extra){
if((fp = fopen(filename, "r"))==NULL){
exit(0);
}
extra = &filename[strlen(filename)-4];
*extra = '\0';
strcat(filename, ".sol1");
return fp;
}
/*
* Function:
* filePrint
*
* Description:
* Writes the output file;
*
* Arguments:
* Int that holds the value that we want to write on the output file
* Pointer to an array that contains the name of the file;
*
* Return value:
* Void;
*
*/
void filePrint(int value, char *filename){
FILE *fp;
if((fp = fopen(filename, "a"))==NULL){
exit(0);
}
fprintf(fp, "%d\n", value);
fprintf(fp, "\n");
fclose(fp);
return;
}
/*
* Function:
* freeB
*
* Description:
* Free of the board
*
* Arguments:
* Double pointer to the board;
* Pointer to an array that contains the size of the board;
*
* Return value:
* Void;
*
*/
void freeB(int **bd, int *size){
int i;
for(i = 0; i<size[0]; i++){
free(bd[i]);
}
free(bd);
return;
}
/*
* Function:
* read
*
* Description:
* Reads the input file;
*
* Arguments:
* Pointer to the file;
* Char that holds the type of value that we wnat to output;
* Pointer to void;
* Double pointer to the board;
* Pointer to an anrray that contains the size of the board;
* Int that holds the information of if we have already allocated the board;
*
* Return value:
* Void;
*
* Other Information:
* The pointer to void will have the target variable that later will be recasted to the respected type;
*
*/
void read(FILE *fp, char type, void* addr, int **bd, int *size, int free){
if(type == 'd'){
if(!fscanf(fp, "%d", (int*)addr)){
fclose(fp);
if(free){
freeB(bd, size);
}
exit(0);
}
}else if(type == 's'){
if(!fscanf(fp, "%s",(char*)addr)){
fclose(fp);
if(free){
freeB(bd, size);
}
exit(0);
}
}
}