-
Notifications
You must be signed in to change notification settings - Fork 0
/
export.c
84 lines (74 loc) · 2.44 KB
/
export.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
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "datastructs.h"
#include "export.h"
#include "genetic.h"
void export_plain(Group *grps, int groupCount) {
int i, e;
/* opening/creating the txt file that the data is getting printeted to*/
FILE* fp = fopen("generated-groups.txt", "w");
/*if the file can't open/create the txt file this error vil be printed, maybe create possibility for looping */
if(fp == NULL) {
printf("Error: could not open 'generated-groups.txt'\n");
return;
}
/* printing data to the finale file, with the given info*/
fprintf(fp,"Groups generated: %d\n", groupCount);
for (i = 0; i < groupCount; i++) {
if (i != 0) fprintf(fp,"\n");
fprintf(fp,"Gruppe %d \n", grps[i].groupNumber +1);
for (e = 0; e < grps[i].memberCount; e++) {
fprintf(fp,"%s\n", grps[i].members[e].name);
}
}
printf("Exported generated groups to human readable textfile \"generated-groups.txt\"\n\n");
/* close the created file pointer */
fclose(fp);
}
/*exports data to csv file*/
void export_to_csv (Group *groups, int groupCount) {
int max_members;
FILE *fp;
/*create output file*/
fp = fopen("result.csv","w");
assert(fp != NULL);
/*find max number of members amongst the groups*/
max_members = find_max_members (groups, groupCount);
/*prints first row (header)*/
make_header(groupCount, fp);
/*prints all other rows (members)*/
print_members(groups, groupCount, max_members, fp);
printf("Exported generated groups to \"result.csv\"\n\n");
fclose(fp);
}
/*finds the max number of members in a group*/
int find_max_members(Group *groups, int groupCount) {
int i, max = 0;
for (i = 0; i <= groupCount; i++) {
if (groups[i].memberCount > max) {
max = groups[i].memberCount;
}
}
return max;
}
/*prints header to csv file*/
void make_header(int groupCount, FILE *fp) {
int i;
fprintf(fp, ";");
for (i = 0; i < groupCount; i++) {
fprintf(fp, "%s %d;","Gruppe", (i+1));
}
fprintf(fp,"\n");
}
/*prints all the members to the csv file*/
void print_members(Group *groups, int groupCount, int max_members, FILE *fp) {
int i, j;
for (i = 0; i < max_members; i++) {
fprintf(fp, "Medlem %d;", (i+1));
for (j = 0; j < groupCount; j++) {
fprintf(fp, "%s;",groups[j].members[i].name);
}
fprintf(fp,"\n");
}
}