I Think I am having a problem with reallocing the token character pointer, please help. i googged how to realloc but I am not sure if this is the right way to do it because I am getting MEMORY CORRUPTION error when I run my program.
char* token = (char*)malloc(sizeof(char));
token[0] = '\0';
int c;
do{
c = fgetc(fp);
if(isalnum(c)){ //add to char array
if(isalpha(c))
c = tolower(c);
if(token[0] == '\0'){
token[0] = (char)c;
token[1] = '\0';
}
else{
token = (char*)realloc(token, strlen(token)+2);
int len = strlen(token);
token[len] = (char)c;
token[len+1] = '\0';
}
}
else{ //token
if(token[0] != '\0'){ //add token
struct token* newtoken = (struct token*)malloc(sizeof(struct token));
newtoken->token = (char*)malloc(strlen(token)*sizeof(char));
strcpy(newtoken->token, token);
newtoken->records = NULL;
struct record* newrecord = (struct record*)malloc(sizeof(struct record));
newrecord->fileName = (char*)malloc(strlen(fileName)*sizeof(char));
strcpy(newrecord->fileName, fileName);
newrecord->freq = 1;
tokens = (struct token*)addToken(tokens, newtoken, newrecord);
}
token[0] = '\0';
}
if(feof(fp))
break;
}while(1);