-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathModel.c
More file actions
160 lines (129 loc) · 3.1 KB
/
Model.c
File metadata and controls
160 lines (129 loc) · 3.1 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
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
#include "DataShell.h"
/*
void read (float *arr1, float *arr2, floar *arr3)
{
char buff[255];
char * value;
float *ptr = NULL;
int cont = 0, n =0;
int flag =0;
FILE *file = openMyfile(fileName, 'r');
while(!feof(file))
{
fscanf(file, "%s", buff);
value = strtok(buff, ",");
while(value != NULL)
{
ptr[cont] = atof(value); //Stores value into ptr
cont ++;
value = strtok(NULL, ",");
}
if(flag == 0)
{
//free(ptr);
flag = 1;
}
else
{
Matrix[n] = ptr; //Matrix now points to ptr
n ++;
}
cont = 0;
}
closeMyfile(file);
}*/
//Function that prints a Matrix
void printMatrix (float **Matrix, int *numLines, int *numElements)
{
for(int i = 0; i < *numLines; i++)
{
for(int j = 0; j < *numElements; j ++)
{
printf("%.6f ", *((*(Matrix+i))+j));
}
printf("\n");
}
}
//Function that reads DB and stores it in a Matrix
void readDB (float **Matrix, char *fileName, int numElements)
{
char buff[255];
char * value;
float *ptr = NULL;
int cont = 0, n =0;
int flag =0;
FILE *file = openMyfile(fileName, 'r');
while(!feof(file))
{
fscanf(file, "%s", buff);
//numElements = countElements(buff);
float *ptr = malloc(sizeof(float) * numElements);
value = strtok(buff, ",");
while(value != NULL)
{
ptr[cont] = atof(value); //Stores value into ptr
cont ++;
value = strtok(NULL, ",");
}
if(flag == 0)
{
//free(ptr);
flag = 1;
}
else
{
Matrix[n] = ptr; //Matrix now points to ptr
n ++;
}
cont = 0;
}
closeMyfile(file);
return;
}
//Function that tells how many elements does a csv line has
int countElements(char *fileName)
{
int numElements = 1;
char buff[255];
FILE *file = openMyfile(fileName, 'r');
fscanf(file, "%s", buff);
char* value = strtok(buff, ",");
while(value != NULL)
{
numElements ++;
value = strtok(NULL, ",");
}
//printf("num = %d ", numElements);
closeMyfile(file);
return numElements;
}
//Function that opens a file
FILE* openMyfile (char *fileName, const char mode)
{
FILE *file = fopen(fileName, &mode);
if(file == NULL)
{
printf("Couln´t open file\n");
exit(0);
}
return file;
}
//Function that closes a file
void closeMyfile(FILE *file)
{
fclose(file);
}
//Function that return number of lines
int NumberLines(char *fileName)
{
int numLines = -2;
char buff[255];
FILE *file = openMyfile(fileName, 'r');
while(!feof(file))
{
fscanf(file, "%s", buff);
numLines ++;
}
closeMyfile(file);
return numLines;
}