Back with more questions about my code.
1) fscanf: When using fscanf do we need to iterate through what it is writing to the buffer to check for the null signifier so we can jump in and strcpy that buffer into our new node?
2) char arrays and strcpy: I created a buffer array of 46 values. Since we know the largest possible word will be 45+Null signifier. Does strcpy automatically detect the NULL signifier and stop copying at that point? Also do I need to free my char array after every single strcpy or will it just get overwritten by the next iteration of fscanf?
3) hash values: From what I understand the hash function will return the index location of whatever index my string belongs to. Then I need to take the address contained within that index location, forward it to my node *next pointer, and then reset that index locations pointer to my current node.
Figuring out how to store the pointer stored within the index returned by the hash function and then pass it to my node* next and then re-allocate that node as the head is where I think I am getting tripped up.
I think I am starting to understand it, but it's very difficult to know if this is all just absolute garbage or not since I am not really sure how to even verify if what I have written so far is accurate.
// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
// TODO
// Open Dictionary File
FILE *dict = fopen(dictionary, "r");
if (dict == NULL)
{
printf("Could not open file");
return false;
}
// Create a buffer array with 46 values
char buffer[45];
while(fscanf(dict, "%s" , buffer) != EOF){
// Iterate through string to know when to stop copying with fscanf
for(int i = 0; i < 45; i++){
// When we find a null character we know the word is completely read into buffer
if(buffer[i] == '\0'){
// Malloc for a new node
node *node = malloc(sizeof(node));
if(node == NULL){
printf("Could not allocate memory for *node");
return false;
}
// Copy the contents of buffer into the new node
strcpy(node->word, buffer);
// Get the hash value of the node
????? = hash(node->word);
}
}
}
return false;
}
Thanks for all your time and effort.