Hi, I'm trying to make a program that writes words to a text. The words are generated by a brute force program. I am getting a segmentation fault 11 but i don't know where i went wrong. Please help! Thanks!
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
static const char alphabet[] =
"abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"1234567890";
static const int alphabet_size = sizeof(alphabet) - 1;
void brute_impl(char * str, int index, int max_depth)
{
int i;
int sys;
FILE *fp;
fp=fopen("~/Desktop/passlist.txt", "w+");
for (i = 0; i < alphabet_size; ++i)
{
str[index] = alphabet[i];
if(index == max_depth-1)
{
fprintf(fp, "%s\n", str);
fclose(fp);
}
else
{
brute_impl(str, index + 1, max_depth);
}
}
}
void brute_sequential(int max_len)
{
char * buf = malloc(max_len + 1);
int i;
for (i = 1; i <= max_len; ++i)
{
memset(buf, 0, max_len + 1);
brute_impl(buf, 0, i);
}
free(buf);
}
int main(void)
{
brute_sequential(2);
return 0;
}