As @unwind said, code is referencing uninitialized data before writing to it
size_t lenn = strlen(pb[i].name); // pb[i].name contents are not defined yet.
...
fgets(pb[i].name, sizeof(pb[i].name), db);
Suggest creating a function to handle the reading of the line.
void ReadLine(FILE *db, char *dest, size_t size) {
char buffer[size+2];
dest[0] = '\0';
if (fgets(buffer, sizeof buffer, db) != NULL) {
size_t len = strlen(buffer);
// Get rid of potential \n
if (len > 0 && buffer[len-1] == '\n') buffer[--len] = '\0';
strncpy(dest, buffer, size);
}
}
for(i = 0; i<cc ; i++) {
ReadLine(db, pb[i].name, sizeof pb[i].name);
ReadLine(db, pb[i].address, sizeof pb[i].address);
ReadLine(db, pb[i].cellphone, sizeof pb[i].cellphone);
ReadLine(db, pb[i].email, sizeof pb[i].email);
}
Additions could be made to ReadLine()
to return EOF on NULL read, excessively long lines or \r
concern as raised by @MadHatter. It's all in one function, so easier to maintain and enhance code.