-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgen_data.cpp
59 lines (52 loc) · 1.25 KB
/
gen_data.cpp
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, char *argv[])
{
int rand_num = 0, final_num;
int type, len, i;
char *str = NULL;
FILE *fp;
scanf("%d%d", &type, &len);
str = (char *)calloc(len+5, 1);
if (str == NULL) {
printf("memory calloc failed!\n");
return -1;
}
srand (time(NULL));
for (i = 0; i < len; i++) {
rand_num = rand();
if (type == 0)
{
final_num = rand_num % 106 + 21;
*(str+i) = (char)final_num;
}
else
{
final_num = rand_num % 3;
switch(final_num)
{
case 0:
*(str+ i) = rand_num%26 + 'a';
break;
case 1:
*(str+ i) = rand_num%26 + 'A';
break;
case 2:
*(str+ i) = rand_num%10 + '0';
break;
}
}
}
// printf("%s\n", str);
fp =fopen("else.txt", "w");
if (fp == NULL){
printf("open file failed\n");
return -1;
}
fwrite(str, 1, strlen(str), fp);
fclose(fp);
free(str);
return 0;
}