I would like to create a hash table by reading the number of entries from a file, file size may be in KB’s or MB’s or even GB’s,
For example: My File has below data
person1_name,person1_father,person1_mother
:
:
personK_name, personK_father, personK_mother
Every entry will be terminated by a new line.
As text file behaviour varies from system to system , I would like to use binary files.
I can get the file size by
1 2 3 4
|
size = ftell(fp,0,SEEK_END), then allocate memory by
char *buffer = malloc(size); and the contents of file using
fread(buffer,1,size,fp);
|
challenge here is at the same time i have to parse the file for number of entries
1 2 3 4 5 6 7 8 9 10 11
|
parseBuffer(char *buffer)
{
char * line = getLine(buffer);
Person *temp_p = getPersonDetails(line);
creatAHashTable(temp_p->person_name, Person)
}
unsigned int GetHashKey(char *person_name)
{
/* some process goes here*/
return () % NUMBER_OF_ENTRIES;
}
|
Now, how will i calculate NUMBER_OF_ENTRIES at the same time and create a hash table?
I can parse the entire buffer to calculate number of entries, but again, i will have to read the entire buffer for creating my
|
Person(name,fathername,mothername);
|
so I will have to read the file two times , once to get the count and then to create my hash table.
Is there some better design to achieve this?