I'm trying to compare a string to a value in an array but it is not working. I think I might be pointing to something other than the word in memory because when I try to print out the value in the array before going into the for loop the output is symbols.
const char *keywords[] = {}\I put 32 keywords are in this array I just didn't want to cluster the code with the list of them.
char w[30];
int true;
char **identifier;
char **temp;
int count;
int n;
int idCount;
void getFile(){
f = fopen(fileName, "r");
if((fscanf(f, "%29[a-zA-Z]", w)) == 1){
int i;
for(i = 0; i < (sizeof(keywords)/sizeof(keywords[0])); i++){
if(strcmp(keywords[i], w) == 0){
//printf("Keyword :");
//printf("%s\n", w);
keywordCount++;
true = 1;
break;
}
else if(true == 1){
count++;
idCount++;
temp = realloc(identifier, count*sizeof(char *)); // another word
identifier = temp;
identifier[count -1] = malloc(strlen(w)+1);
strcpy(identifier[count-1], w);
true = 0;
break;
}
else {
for(n = 0; n < count; n++){
//printf("%s\n", identifier[n]);
if(strcmp(identifier[count],w)==0){
printf("%s\n", identifier[n]);
idCount++;
break;
}
}
}
}
for(n = 0; n < count; n++){
free(identifier[n]);
}
free(identifier);
}
}
fclose(f);
}
Aucun commentaire:
Enregistrer un commentaire