Hi all,
I am trying to implement chain hashing , but getting a small problem. My hash function
is to sum the ascii value of category entered by the user and then subtract it by 300.
for ex if user entered the category as "unix" then my equivalent hash function value
is = 152. Now it is working fine when i am inserting 2 records with same category
name. but it is giving me a problem when i insert another record with same category
name. problem is it is giving the value of b.next(structure member) as 0 before
writing the record and after writing the record, that is the problem , if it write the
same value which is holding then problem is solved .but it is writing 0. i am tring to
remove this from past 2 days ,but couldnt able to find the minstake.i m using turbo
borland c compiler.
Thanx in advance
#include<stdio.h>
#include<conio.h>
#include<string.h>
void libing();
void main()
{
libing();
}
void libing()
{
struct {
char name[20];
char author[20];
char category[20];
long int bookid;
char status;
long int id;
int next;
}b,b1;
FILE *fb;
int choice;
int position=0,flag,sum=0,i=0;
long int tempbook=10000;
int temp=0;
printf("\nLibrary Management System");
printf("\n1)Add Book\n2)Issue Book\n3)Return Book\n4)Mail\n5)Sign Out");
printf("\nEnter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1: /*Inserting*/
fb=fopen("bookdb.dat","rb+");
if(fb==NULL)
{
printf("book file not found");
fb=fopen("bookdb.dat","wb+");
while(position!=1800)
{
strcpy(b.name,"");
strcpy(b.author,"");
strcpy(b.category,"");
b.status=' ';
b.bookid=0;
b.id=0;
b.next=0;
fwrite(&b,sizeof(b),1,fb);
position=position+1;
}
getch();
fclose(fb);
}
else
{
while(fread(&b,sizeof(b),1,fb)==1)
position=position+1;
}
fclose(fb);
fflush(stdin);
printf("\n\nEnter name of Book : ");
gets(b.name);
fflush(stdin);
printf("\n\nEnter Author of Book : ");
gets(b.author);
fflush(stdin);
printf("\n\nEnter Category of Book : ");
gets(b.category);
b.status='N';
b.id=0;
b.next=0;
b.bookid=tempbook+position+1;
b1=b;
while(b.category[i]!='\0')
{
sum=sum+b.category[i];
i++;
}
sum=sum-300;
printf("\nsum=%d",sum);
fb=fopen("bookdb.dat","rb+");
fseek(fb,(sum-1)*sizeof(b),SEEK_CUR);
fread(&b,sizeof(b),1,fb);
if(b.bookid!=0)
{
if(b.next==0) /*only 1 record on partcular category*/
{
b.next=position+1;
fclose(fb);
fb=fopen("bookdb.dat","rb+");
fseek(fb,(sum-1)*sizeof(b),SEEK_CUR);
// fseek(fb,-(sizeof(b)),SEEK_CUR);
fwrite(&b,sizeof(b),1,fb);
fclose(fb);
fb=fopen("bookdb.dat","rb+");
while(!feof(fb))
fgetc(fb);
fwrite(&b1,sizeof(b),1,fb);
}
else /*more than 1 rcord on particular category*/
{
while(b.next!=0)
{
fclose(fb);
fb=fopen("bookdb.dat","rb+");
fseek(fb,(b.next-1)*sizeof(b),SEEK_CUR);
temp=b.next-1;
fread(&b,sizeof(b),1,fb);
// b1=b;
if(b.next==0)
{
b.next=position+1;
// printf("bnext=%d",b.next);
fclose(fb);
fb=fopen("bookdb.dat","rb+");
// printf("\nbnext=%d",b.next);
fseek(fb,temp*sizeof(b),SEEK_CUR);
printf("i came inside");
// b.next=position+1;
fwrite(&b,sizeof(b),1,fb);
printf("bnext=%d",b.next);
fseek(fb,-(sizeof(b)),SEEK_CUR);
fread(&b,sizeof(b),1,fb);
printf("bnext=%d",b.next);
fclose(fb);
fb=fopen("bookdb.dat","rb+");
while(!feof(fb))
fgetc(fb);
fwrite(&b1,sizeof(b),1,fb);
break;
}
// else
// b.next=b1.next;
}
}
}
else /*no record on particular category*/
{
fclose(fb);
fb=fopen("bookdb.dat","rb+");
fseek(fb,(sum-1)*sizeof(b),SEEK_CUR);
//fseek(fb,-(sizeof(b)),SEEK_CUR);
//printf("\npos=%ld",ftell(fb));
//printf("\nrecsize=%ld\n",sizeof(b));
printf("\nauthor=%s,category=%s\n",b1.author,b1.category);
fwrite(&b1,sizeof(b),1,fb);
}
fclose(fb);
}
printf("\nim leaving");
getch();
}
---------------------------------
Do you Yahoo!?
Check out the new Yahoo! Front Page. www.yahoo.com
[Non-text portions of this message have been removed]
------------------------ Yahoo! Groups Sponsor --------------------~-->
Make a clean sweep of pop-up ads. Yahoo! Companion Toolbar.
Now with Pop-Up Blocker. Get it for free!
http://us.click.yahoo.com/L5YrjA/eSIIAA/yQLSAA/EbFolB/TM
--------------------------------------------------------------------~->
>-----------------------------------------~-~>
CHECK THE ARCHIVE BEFORE POSTING!!!! Archive is available at
http://www.eScribe.com/software/C-Paradise/
>------------------------------------------_->
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/C-Paradise/
<*> To unsubscribe from this group, send an email to:
[EMAIL PROTECTED]
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/