-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfiles.c
99 lines (86 loc) · 3.2 KB
/
files.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
#include "spotPrices.h"
#include "electricVehicle.h"
#include "files.h"
#include "allError.h"
/*Funktionen readFile læser datafilen, og lægger det læste data ind i elPrArray*/
int readSpotPricesFile(spotPrices elPrArray[], int elPrArrayLen) {
FILE *fpointer;
char singleline[FILE_LINE_LENGTH];
int i = 0;
int numOfLineData = 0;
fpointer = fopen("output.csv", "r"); /*Åbner fil i readmode, derfor: "r"*/
if (fpointer != NULL) {
while (!feof(fpointer)){
fgets(singleline, FILE_LINE_LENGTH, fpointer); /*Der læses én linje ad gangen*/
numOfLineData = sscanf(singleline, "%[^;];%f", elPrArray[i].date, &elPrArray[i].price);
if (numOfLineData != 2)
printError(401, "output.csv");
i++;
}
}
else
printError(404, "output.csv");
return fclose(fpointer);
}
/* Laver et array med options fra: "readOptionsFile". */
int getOption(enum options option) {
/* Laver statisk lagerallokering sådan arrayet ikke slettes hver gang funktionen kaldes. */
static enum options optionsArray[OPTIONS_LENGTH];
static int isFileRead;
if (!isFileRead) {
if(readOptionsFile(optionsArray))
printError(409, "options.txt");
isFileRead = 1;
}
return optionsArray[option];
}
/* Læser data om elbiler fra EVdata.csv, og lægger dataen i et array*/
int readFileEV(electricVehicle array[], int arrayLen){
FILE *fpointer = fopen("EVdata.csv", "r");
char singleline[FILE_LINE_LENGTH];
int i = 0;
int numOfLineData = 0;
if (fpointer != NULL){
while (!feof(fpointer)){
fgets(singleline, FILE_LINE_LENGTH, fpointer);
numOfLineData = sscanf(singleline, "%[^;]; %f; %f; %d; %f",
array[i].modelName,
&array[i].capacity,
&array[i].chargeRate,
&array[i].numOfEV,
&array[i].kmPrKwh);
if (numOfLineData != 5)
printError(401, "EVdata.csv");
else if (array[i].numOfEV < 0)
printError(402, "EVdata.csv - numOfEV");
else if (array[i].capacity <= 0 || array[i].chargeRate <= 0 || array[i].kmPrKwh <= 0 )
printError(403, "EVdata.csv - capacity or chargeRate or kmPrKwh");
i++;
}
}
else
printError(404, "EVdata.csv");
return fclose(fpointer);
}
/*Læser tekstfilen med options, og lægger data derfra ind i et optionsArray*/
int readOptionsFile(enum options optionsArray[]) {
FILE *fpointer = fopen("options.txt", "r");
char singleline[FILE_LINE_LENGTH];
int numOfLineData = 0;
int lineData = 0;
int i;
if (fpointer != NULL) {
for (i = 0; i < OPTIONS_LENGTH; i++) {
fgets(singleline, FILE_LINE_LENGTH, fpointer);
numOfLineData = sscanf(singleline, "%*[^:]:%d", &lineData);
if (lineData < 0)
printError(402, "options.txt");
optionsArray[i] = lineData;
if (numOfLineData != 1)
printError(401, "options.txt");
}
}
else
printError(404, "options.txt");
return fclose(fpointer);
}