-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_students.c
154 lines (133 loc) · 3.53 KB
/
generate_students.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
#ifndef GENERATE
#define GENERATE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <assert.h>
#include "student.h"
// Function to load names from a file into an array
unsigned int randval()
{
unsigned int randval;
FILE *f;
f = fopen("/dev/random", "r");
fread(&randval, sizeof(randval), 1, f);
fclose(f);
return randval;
}
void loadNamesFromFile(char* filename, char array[][MAX_NAME_LEN], int numNames)
{
FILE *file = fopen(filename, "r");
for (int i = 0; i < 20; i++)
{
char ch = fgetc(file);
int j = 0;
while (ch != EOF)
{
//printf("%c", ch);
if (ch == '\n')
{
array[i][j] = '\0';
break;
}
array[i][j] = ch;
ch = fgetc(file);
j++;
}
// fgets(array[i], MAX_NAME_LEN, file);
}
for (int i = 0; i < numNames-1; i++)
{
array[i][strlen(array[i]) - 1] = '\0';
}
fclose(file);
}
char* generateFullName(char first_names[20][MAX_NAME_LEN], char last_names[20][MAX_NAME_LEN])
{
srand(time(NULL));
int i = randval()%20;
int j = randval()%20;
char *name = malloc(sizeof(char)*105);
sprintf(name, "%s %s", first_names[i], last_names[j]);
//printf("%s", name);
return name;
}
char* getRandomString(char names[][MAX_NAME_LEN], int range)
{
srand(time(NULL));
int num = randval() % range;
return names[num];
}
struct Student generateStudent(int id)
{
struct Student student;
char firstNames[20][MAX_NAME_LEN];
char lastNames[20][MAX_NAME_LEN];
char hostelNames[5][MAX_NAME_LEN];
char courses[5][MAX_NAME_LEN];
loadNamesFromFile("names/firstname.txt", firstNames, 20);
loadNamesFromFile("names/lastname.txt", lastNames, 20);
loadNamesFromFile("names/hostelnames.txt", hostelNames, 5);
loadNamesFromFile("names/coursenames.txt", courses, 5);
srand(time(NULL));
student.id = id;
strcpy(student.name, generateFullName(firstNames, lastNames));
strcpy(student.hostel, getRandomString(hostelNames, 5));
strcpy(student.course, getRandomString(courses, 5));
student.roomNumber = randval() % 400 + 101;
sprintf(student.dob, "%d/%d/%d", randval() % 28 + 1, randval() % 12 + 1, randval() % 16 + 1985);
student.yearOfStudy = randval() % 5 + 1;
return student;
}
void registration(struct Student student)
{
FILE *file = fopen("disk", "a");
if (file == NULL)
{
perror("Error opening database file");
exit(0);
}
fwrite(&student, 1, sizeof(struct Student), file);
fclose(file);
}
double GetTime()
{
struct timeval t;
int rc = gettimeofday(&t, NULL);
assert(rc == 0);
return (double) t.tv_sec + (double) t.tv_usec/1e6;
}
void Spin(int howlong)
{
double t = GetTime();
while ((GetTime() - t) < (double) howlong); // do nothing in loop
}
void generate()
{
FILE *file = fopen("disk", "a");
if (file == NULL)
{
perror("Error opening database file");
exit(0);
}
fseek(file, -sizeof(struct Student), SEEK_CUR);
struct Student temp;
fread(&temp, 1, sizeof(struct Student), file);
if (temp.id >= 100)
{
fclose(file);
return;
}
for (int i = 0; i < 100; i++)
{
struct Student student = generateStudent(i+1);
//Spin(1);
registration(student);
}
printf("Registration complete for 100 students.\n");
fclose(file);
}
#endif