I'm certain that the error is from fgets. The dictionary I'm using for this pset is preprocessed(?) so that all the words are ≤ 8.
I had a segmentation fault earlier with copying the first 2 characters from the user inputted password. After searching, I found I could solve it by using memset to fill all 3 chars with terminating characters.
I tried the same thing with this seg fault, but to no avail. I also tried changing the file pointer to one in which I have only one line, 7 characters in length→ this didn't work either.
#define _XOPEN_SOURCE
#include <crypt.h>
#include <cs50.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main (int argc, char* argv[])
{
FILE* crackDict = fopen("/Dropbox/CS50/hacker2/CrackDictionary/Cracked_Dictionary.txt", "r");
string encryptedPass, crackWord, salt = NULL, engWord = NULL;
if ((argc == 2) && (strlen(argv[1]) == 13))
{
encryptedPass = argv[1];
}
else
{
printf("Usage: ./Crack [encrypted password...13 chars long]\n");
return 0;
}
//clear the salt with terminating chars and copy first 2 chars of userinput
memset(salt, '\0', 3);
strncpy(salt, encryptedPass, 2);
memset(engWord, '\0', 9);
for (int i = 0; fgets (engWord, 9, crackDict)!= NULL; i++)
{
crackWord = crypt(engWord, salt);
if (strcmp(crackWord, encryptedPass) == 0)
{
break;
}
else
memset(engWord, '\0', 9);
}
printf("%s\n", engWord);
return 0;
}
Any help is greatly appreciated.