I want to use this program to write a hash table to a file
but i get
cannot convert parameter 1 from ‘fpinfo’ to ‘const void *’
error at compile time
if i change struct fpinfo e; with struct fpinfo *e i get the run time error:
The variable ‘e’ is being used without being initialized.
I tried to initialize e by declaring it as struct fpinfo *e=NULL;. This either doesn’t work.
Please give me your help as usual.
WriteHTtoFile(struct fpinfo t[][B_ENTRIES],int this_tanker,tanker_record tr[][BUCKETS])
{
//struct fpinfo *e;
struct fpinfo e;
int i=0, mask;
char filename[sizeof ("file0000.txt")];
sprintf(filename, "filee%d.txt", this_tanker);
curr_tanker++;
//fp = fopen(filename,"w");
FILE *htfile=fopen(filename,"w+b");
system("cls");
if (htfile != NULL)
{
for (int j = 0; j < BUCKETS; ++j)
{
for (int k = 0; k < tr[this_tanker][j].bkt.num_of_entries; ++k)
{
printf("%d\t%d\t%s\n",t[j][k].chunk_offset,t[j][k].chunk_length,t[j][k].fing_print);
(e).chunk_offset=t[j][k].chunk_offset;
(e).chunk_length=t[j][k].chunk_length;
strcpy((char*)((e).fing_print),(char*)t[j][k].fing_print);
fwrite(e,sizeof(fpinfo),1,htfile);
}
}
fclose(htfile);
}
else
{
std::cout<<"File could not be opend for writing";
printf("Error %d\t\n%s", errno,strerror(errno));
}
fclose(htfile);
return 1;
}
The first argument to
fwrite()is aconst void*. This passes astruct fpinfo:Change to:
I am unsure what the members of
struct fpinfoare so this may be unsafe (if, for example, it contains any pointer members). Any future reordering of the members instruct fpinfoor a change that results in a size increase ofstruct fpinfo(like new members being added) means any attempt to read previously writtenstruct fpinfodata will be incorrect.When the declaration of
ewas changed to astruct fpinfo* e;the unitialised error was due to the pointer not being NULL or being assigned to a dynamically allocatedstruct fpinfo.When changed to
struct fpinfo *e = NULL;this would cause a segmentation fault when an attempt to access any member ofeoccurred as it is not pointing to astruct fpinfo.