-
Notifications
You must be signed in to change notification settings - Fork 0
/
Source.c
276 lines (236 loc) · 6.54 KB
/
Source.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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/**
* @file Source.c
* @author Kornelijus Petronis
* @brief
* @version 1.3
* @date 2019-12-12
*
* @copyright Copyright (c) 2019
*
*
* <<<<# This program read bank customers list from csv file and create two new files #>>>>
*
* First file is all clients sorted from smallest to biggest by bank balance and in the begin of list is number of clients.
*
* Second file is only for one client what you searching.
*
*/
// Library list
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define fileName "data2.csv"
int sizeOfArray;
/**
* @brief Structure
*
*/
struct personalData
{
char address[100];
long int phone;
char birthday[20];
};
struct bank_custumers_details
{
char name[20];
char IBAN[30];
double balance;
struct personalData pData;
};
// Prototypes
void readFile(struct bank_custumers_details bank[sizeOfArray]);
int countRows();
void sortByBalance(struct bank_custumers_details bank[sizeOfArray]);
void writeToFileSorted(struct bank_custumers_details bank[sizeOfArray]);
void writeToFileOnly(struct bank_custumers_details bank[sizeOfArray]);
int main()
{
sizeOfArray = countRows() + 1;
struct bank_custumers_details bank[sizeOfArray];
readFile(bank);
sortByBalance(bank);
writeToFileSorted(bank);
writeToFileOnly(bank);
for (int i = 2; i < sizeOfArray; i++)
{
// printf("%s\n", bank[i].IBAN);
// printf("%.2lf | %s | %s | %s\n", bank[i].balance, bank[i].name, bank[i].pData.address, bank[i].IBAN);
}
return 0;
}
// ==============================================================================================
// ==============================================================================================
/**
* @brief write to file only found clients.
*
* @param bank
*/
void writeToFileOnly(struct bank_custumers_details bank[sizeOfArray])
{
char name[20];
int state = 0;
FILE *fptr;
fptr = (fopen("Clients_Searching.txt", "w"));
if (fptr == NULL)
{
printf("Error!");
exit(1);
}
printf("Write name of client you searching: ");
scanf("%s", &name[0]);
for (int i = 2; i < sizeOfArray; i++)
{
if (!strcmp(bank[i].name, name))
{
fprintf(fptr, "===========================\nName: %s\nIBAN: %s\nBalance: %.2lf\n-------------------------\nAddress: %s\nPhone: %ld\nBirthday: %s\n===========================\n\n", bank[i].name, bank[i].IBAN, bank[i].balance, bank[i].pData.address, bank[i].pData.phone, bank[i].pData.birthday);
state++;
}
}
fclose(fptr);
if (!state)
printf("No clients with this name. :(\n");
else
printf("OK!\n%d was found!\n", state);
}
/**
* @brief Write to file all clients sorted by balance
*
* @param bank
*/
void writeToFileSorted(struct bank_custumers_details bank[sizeOfArray])
{
char name[50];
int mark, i, num;
FILE *fptr;
fptr = (fopen("Clients_Sorted.txt", "w"));
if (fptr == NULL)
{
printf("Error!");
exit(1);
}
fprintf(fptr, "Clients list sorted by balance\nClients number: %d\n\n\n", sizeOfArray - 2);
for (i = 2; i < sizeOfArray; ++i)
{
fprintf(fptr, "===========================\nName: %s\nIBAN: %s\nBalance: %.2lf\n-------------------------\nAddress: %s\nPhone: %ld\nBirthday: %s\n===========================\n\n", bank[i].name, bank[i].IBAN, bank[i].balance, bank[i].pData.address, bank[i].pData.phone, bank[i].pData.birthday);
}
fclose(fptr);
}
/**
* @brief Sort bank array by balance
*
* @param bank
*/
void sortByBalance(struct bank_custumers_details bank[sizeOfArray])
{
double t_balance;
long int t_phone;
char t_str[100];
for (int i = 0; i < sizeOfArray; i++)
{
for (int j = i + 1; j < sizeOfArray; j++)
{
if (bank[i].balance > bank[j].balance)
{
t_balance = bank[i].balance;
bank[i].balance = bank[j].balance;
bank[j].balance = t_balance;
strcpy(t_str, bank[i].name);
strcpy(bank[i].name, bank[j].name);
strcpy(bank[j].name, t_str);
strcpy(t_str, bank[i].IBAN);
strcpy(bank[i].IBAN, bank[j].IBAN);
strcpy(bank[j].IBAN, t_str);
strcpy(t_str, bank[i].pData.address);
strcpy(bank[i].pData.address, bank[j].pData.address);
strcpy(bank[j].pData.address, t_str);
strcpy(t_str, bank[i].pData.birthday);
strcpy(bank[i].pData.birthday, bank[j].pData.birthday);
strcpy(bank[j].pData.birthday, t_str);
t_phone = bank[i].pData.phone;
bank[i].pData.phone = bank[j].pData.phone;
bank[j].pData.phone = t_phone;
}
}
}
}
/**
* @brief Count rows(Clients) in csv file.
*
* @return int
*/
int countRows()
{
char buf[1024];
int row_count = 0;
FILE *fp = fopen(fileName, "r");
if (!fp)
{
printf("Can't open file\n");
}
while (fgets(buf, 1024, fp))
{
row_count++;
if (row_count == 1)
{
continue;
}
}
fclose(fp);
return row_count;
}
/**
* @brief Read file and collect all data to bank array
*
* @param bank
*/
void readFile(struct bank_custumers_details bank[sizeOfArray])
{
// struct bank_custumers_details bank[1000];
char buf[1024];
int row_count = 0;
int field_count = 0;
FILE *fp = fopen(fileName, "r");
if (!fp)
{
printf("Can't open file\n");
// return 0;
}
while (fgets(buf, 1024, fp))
{
field_count = 0;
row_count++;
if (row_count == 1)
{
continue;
}
char *field = strtok(buf, ",");
while (field)
{
switch (field_count)
{
case 0:
strcpy(bank[row_count].name, field);
break;
case 1:
strcpy(bank[row_count].IBAN, field);
break;
case 2:
bank[row_count].balance = atof(field);
break;
case 3:
strcpy(bank[row_count].pData.address, field);
break;
case 4:
bank[row_count].pData.phone = atol(field);
break;
case 5:
strcpy(bank[row_count].pData.birthday, field);
break;
}
field = strtok(NULL, ",");
field_count++;
}
}
fclose(fp);
}