我编写了一个代码,用于编写密码字典,但它不写入. txt文件
代码生成一个文本文件,然后使用我定义的字符集写入所有可能的密码,但它不写入文本文件。
这是密码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
void Dictionary_Generator(char *charset, int min, int max) {
char *password = malloc((max + 1) * sizeof(char));
if (password == NULL) {
fprintf(stderr, "Error: memory allocation failed\n");
exit(1);
}
FILE *file = fopen("dictionary.txt", "w");
if (file == NULL) {
fprintf(stderr, "Error: could not create file\n");
exit(1);
}
int charset_length = strlen(charset);
int num_combinations = pow(charset_length, max);
for (int i = 0; i < num_combinations; i++) {
int index = i;
int length = min;
while (index >= charset_length) {
password[length - 1] = charset[index % charset_length];
index /= charset_length;
length++;
}
password[length - 1] = charset[index];
password[length] = '\0';
if (length >= min) {
int result = fprintf(file, "%s\n", password);
if (result < 0) {
perror("Error writing to file");
exit(1);
}
}
}
fclose(file);
free(password);
}
int main() {
char *charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
int min = 4;
int max = 8;
Dictionary_Generator(charset, min, max);
return 0;
}
它生成一个文本文件,但该文件为空。
2条答案
按热度按时间pinkon5k1#
通过在
cc
行的末尾添加-lm
来修复undefined reference to 'pow'
后,出现另一个错误:charset_length
是62。max
是8。因此,当您这样做时:
我们正在执行
pow(62,8)
,当截断为int
时,得到一个负数,pow
的结果大约为2.1e14,对于int
来说太大了。因此,后续的
for
循环将执行 * 零 * 次。您可能希望使用
long long
来代替,但是2e14迭代似乎很多。执行:
看起来更合理(并且 * 适合 *
int
)。但是,在这种情况下,程序只输出空行。当我进行类似类型的迭代时,我使用了一个长度为当前数字宽度的"辅助"向量,我将其视为一个N位宽度的数字(charset_length)。
它接受0到charset_length-1范围内的每个数字的值。
我认为更简单的方法是让外部循环输出字长(例如4 - 8),然后从
charset
生成该长度的所有字因此,我不得不做一些相当大的重构,将生成代码分成多个函数。
实际的运行时非常大,所以我将进度输出添加到
stdout
...下面是重构后的代码,它带有注解:
mspsb9vt2#
这段代码似乎将应该写入文件的字符串赋给了一个变量,但实际上并没有将其写入文件。