-
Notifications
You must be signed in to change notification settings - Fork 1
/
name_generator.c
119 lines (91 loc) · 3.06 KB
/
name_generator.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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
char *getRandomName(int flag){
static char **sampleList;
static int counter = 0;
if(!sampleList){
FILE *sample_file = fopen("namelist.txt", "r+");
if(!sample_file){printf("Erro no arquivo de amostras de nomes!"); exit(0);}
sampleList = (char**) malloc (sizeof(char*));
char *aux = (char*) malloc (sizeof(char)*16);
while(fscanf(sample_file, "%s\n", aux) == 1){
sampleList[counter] = strdup(aux);
counter++;
sampleList = (char**) realloc (sampleList, sizeof(char*)*(counter+1));
}
fclose(sample_file);
}
if(flag)
return sampleList[rand()%counter];
else{
for(int i = 0; i < counter; i++)
free(sampleList[i]);
free(sampleList);
return NULL;
}
}
char *getRandomLastName(int flag){
static char **sampleList;
static int counter = 0;
if(!sampleList){
FILE *sample_file = fopen("lastnamelist.txt", "r+");
if(!sample_file){printf("Erro no arquivo de amostras de sobrenomes!"); exit(0);}
sampleList = (char**) malloc (sizeof(char*));
char *aux = (char*) malloc (sizeof(char)*16);
while(fscanf(sample_file, "%s\n", aux) == 1){
sampleList[counter] = strdup(aux);
counter++;
sampleList = (char**) realloc (sampleList, sizeof(char*)*(counter+1));
}
fclose(sample_file);
}
if(flag)
return sampleList[rand()%counter];
else{
for(int i = 0; i < counter; i++)
free(sampleList[i]);
free(sampleList);
return NULL;
}
}
char *getRandomCourse(int flag){
static char **sampleList;
static int counter = 0;
if(!sampleList){
FILE *sample_file = fopen("courselist.txt", "r+");
if(!sample_file){printf("Erro no arquivo de amostras de cursos!"); exit(0);}
sampleList = (char**) malloc (sizeof(char*));
char *aux = (char*) malloc (sizeof(char)*16);
while(fscanf(sample_file, "%s\n", aux) == 1){
sampleList[counter] = strdup(aux);
counter++;
sampleList = (char**) realloc (sampleList, sizeof(char*)*(counter+1));
}
fclose(sample_file);
}
if(flag)
return sampleList[rand()%counter];
else{
for(int i = 0; i < counter; i++)
free(sampleList[i]);
free(sampleList);
return NULL;
}
}
int main(){
FILE *data_file = fopen("entries.txt", "w+");
int quantity, i;
srand(time(NULL));
printf("Digite a quantidade de registros que devem ser criados:\n");
scanf("%d", &quantity);
for(i = 0; i < quantity; i++)
fprintf(data_file, "1\n%d\n%s\n%s\n%s\n%.2f", (i+200000), getRandomName(1), getRandomLastName(1), getRandomCourse(1), (float)(rand()%11));
if(data_file) printf("Criado arquivo entries.txt com %d registros!\n", quantity);
fclose(data_file);
getRandomName(0);
getRandomLastName(0);
getRandomCourse(0);
return 0;
}