Hello,
I'm trying to read dictionary words from a file and store the output in a struct element of type unsigned char *. I keep getting a bunch of memory related errors and segmentation faults which Im sure are totally related to me trying to access something that's not really there. I'm just not sure how to get around my errors. Here's my truncated code (the parts that concern my problem so far)
What I would like to do eventually is to calculate the hash value of a given word, which is argv[2] in this example, and calculate the hash value of each word in the given filename (argv[1]), and compare whether they match or no. If match, display "found", otherwise, "not found".
#include <stdlib.h>
#include <stdio.h>
#include <cuda.h>
#include <cutil.h>
#include "common.h"
#define MAX_THREADS_PER_BLOCK 128
typedef struct {
unsigned const char *data;
unsigned const char *hash;
} testvector;
int main(int argc, char *argv[])
{
if(argc!=3)
printf("usage: ./sha1test <filename> <searchword>\n");
else{
FILE* fp_in = fopen(argv[1], "r");
int count = 0;
testvector *tv;
tv = (testvector *) malloc(sizeof(testvector));
if(!fp_in)
printf("can't open file\n");
unsigned char gHash[20];
sha1_cpu ((unsigned char*)argv[2], strlen((const char*)argv[2]), gHash);
printf("Your word is:\t%s\n",argv[2]); //display <searchword>
printf("The hash is:\t"); //display its calculated hash
int ii;
for(ii=0; ii<20; ii++)
printf("%x", gHash[ii]);
printf("\n");
//read words from file and calculate their hash values
while(!feof(fp_in)){
size_t bytes = 128;
unsigned char d[bytes];
fgets((char *)(&d[0]),bytes,fp_in);
// d = (unsigned char *) malloc(50*sizeof(unsigned char));
count++;
free(d);
}
fclose(fp_in);
}
return 0;
}
Would appreciate any help.