c - Strcmp wont return zero for same two same strings -
i reading in csv file using fscanf has 2 fields.
datatype_t*read(file* fp) { char name[66]; char data[1466]; if (fscanf(fp, "%[^,] %[^\n]", name, data) == 2) { datatype_t *d = (datatype_t*)malloc(sizeof(datatype_t)); d->name = strdup(name); d->data = strdup(data); return d; } return null; }
csv file data following
mr dave,school teacher
mike,head
sachin,staff member
now reading text file has data in it.
char buffer[66]; if (fgets(buffer,sizeof buffer, fp) != null ) { keydata_t *k = (keydata_t*)malloc(sizeof(keydata_t)); size_t len = strlen(buffer); if(buffer > 0 && buffer[len-1] == '\n'){ buffer[--len] = '\0'; } k->name = strdup(buffer); return k; } return null; }
data of txt file looks this.
mr dave
ron
mr tim
now when compare data strings:
new_ptr = root; while((keyt = read_key(keyfile))!= null){ printf("%s\n", keyt->name); printf("%s\n", root->key); if(strcmp(keyt->name, root->key) == 0){ printf("match\n"); } else if(strcmp(keyt->name, root->key) > 0){ printf("not equal\n"); } else if (strcmp(keyt->name, root->key) < 0){ printf("not equal\n"); } new_ptr = search(new_ptr, keyt); }
it keeps printing
not equal
mr dave there should match. cant seem figure out problem two.
from man fgets
:
fgets
reads in @ 1 less size characters stream , stores them buffer pointed s. reading stops after eof or newline. if newline read, stored buffer. terminating null byte (aq\0aq) stored after last character in buffer.
fscanf
does not store newline in buffer.
please try checking last symbol of keyt->name
, remove if newline
Comments
Post a Comment