Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

write to file #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 98 additions & 25 deletions project4/mydatabase.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ int matchSpecs(Tuple ct, Tuple lt) {
}
return 1;
}

}

int hash(char* key) {
Expand All @@ -65,7 +64,7 @@ int hash(char* key) {
for (int i = 0; key[i] != '\0'; i++) {
sum += key[i];
}

return sum % NBUCKETS;
}

Expand Down Expand Up @@ -101,32 +100,46 @@ void insert(Tuple t, Hashtable h) {
printf("inserting...\n");
bucketInsert(t, &(h[hash(t.key)]));
}

}

int cmpTuple(Tuple a, Tuple b) {
/*int cmpTuple(Tuple a, Tuple b) {
if(a.nAttr == b.nAttr && a.key == b.key) {
for(int i=0; i< a.nAttr; i++) {
//returns false if any of the attributes do not match
if(a.attr[i] != b.attr[i]) {
return 0;
}
}
//if the above condition is
//if the above condition is
//never satisfied in the for loop, we return 1
return 1;
} else {
printf("Number of attributes or keys don't match\n");
}

return 0;

}
}*/

void printTuple(Tuple t){
printf("tuple(%s, %d, %s, %s, %s)\n",t.key, t.nAttr, t.attr[0], t.attr[1], t.attr[2]);
}

/*
void filePrintTuple(Tuple t, char *filename){
FILE *fp;
fp = fopen(filename, "a");
fseek(fp, 0, SEEK_END); // goto end of file
{
if (fp == NULL) {
perror("Can't open file");
} else if (ftell(fp) == 0)
{
fprintf(fp, "tuple(%s, %d, %s, %s, %s)\n",t.key, t.nAttr, t.attr[0], t.attr[1], t.attr[2]);}
fclose(fp);
}}*/

void printTable(Hashtable h) {
for(int i=0; i<NBUCKETS; i++) {
Tuple *curr = h[i];
Expand All @@ -137,6 +150,23 @@ void printTable(Hashtable h) {
}
}

//fixed
void filePrintTable(Hashtable h, char *filename) {
FILE *fp;
fp = fopen(filename, "a");
fseek(fp, 0, SEEK_END); // go to end of file
if (ftell(fp) == 0){//if file is empty
for(int i=0; i<NBUCKETS; i++) {
Tuple *curr = h[i];
while(curr != NULL) {
//filePrintTuple(*curr, filename);
{
if (fp == NULL) {
perror("Can't open file");
} else
{
fprintf(fp, "tuple(%s, %d, %s, %s, %s)\n", curr->key, curr->nAttr, curr->attr[0], curr->attr[1], curr->attr[2]);}



void bucketDelete(Tuple t, List *head) {
Expand All @@ -145,7 +175,7 @@ void bucketDelete(Tuple t, List *head) {
} else {
Tuple *curr = *head;
Tuple *trail = NULL;

while(curr != NULL) {
if(matchSpecs(t, *curr)) {
break;
Expand All @@ -161,56 +191,99 @@ void bucketDelete(Tuple t, List *head) {
//case 3 delete from head node
if(*head == curr) {
*head = (*head)->next;

} else {//case 4 delete beyond head
trail->next = curr->next;

}
free(curr);
}

}
}


void delete(Tuple t, Hashtable h ) {
void bucketLookup(Tuple t, List *head) {
if(*head == NULL) {
printf("Cannot lookup from empty list\n");
} else {
Tuple *curr = *head;
Tuple *trail = NULL;

while(curr != NULL) {
if(matchSpecs(t, *curr)) {
printTuple(t);
break;
} else {
trail = curr;
curr = curr->next;
}
}
//case 2 not found tuple
if(curr == NULL) {
printf("tuple was not found\n");
}
free(curr);
}

}


void tupleDelete(Tuple t, Hashtable h ) {
printf("deleting...\n");
bucketDelete(t, &h[hash(t.key)]);
}

void lookup(Tuple t, Hashtable h){
printf("finding tuple...\n");
bucketLookup(t, &h[hash(t.key)]);
}


int main(void) {
Hashtable csg;

for(int i = 0; i< NBUCKETS; i++) {

csg[i] = NULL;
}


Tuple c = tuple_new("John Lennon", 3, "CSC282", "John Lennon", "A-");
insert(c, csg);



Tuple a = tuple_new("Luke Skywalker", 3,"CSC173", "Luke Skywalker", "A");
insert(a, csg);

Tuple b = tuple_new("Luke Skywalker", 3,"MTH201", "Luke Skywalker", "C-");
insert(b, csg);

/*Tuple x = tuple_new("Luke Skywalker", 3,"*", "*", "*");
insert(x, csg);*/

/*Tuple b = tuple_new("Luke Skywalker", 3,"MTH201", "Luke Skywalker", "C-");
insert(b, csg);*/

Tuple d = tuple_new("George Eastman", 3, "MUR110", "George Eastman", "A+");
insert(d, csg);

Tuple e = tuple_new("George Eastman", 3, "*", "*", "*");


Tuple e = tuple_new("George East", 3, "*", "*", "*");
insert(e,csg);

Tuple f = tuple_new("George Eastman", 3, "*", "*", "*");

//printf("a ?= d %d", matchSpecs(e,a));
printTable(csg);
delete(e,csg);
printTable(csg);
//lookup(f,csg);
//tupleDelete(e,csg);
//lookup(e,csg);
//lookup(f,csg);
filePrintTable(csg,"Test0.txt");
tupleDelete(f,csg);
//lookup(d,csg);
//lookup(f,csg);
//lookup(e,csg);
//printTable(csg);

filePrintTable(csg,"Test.txt");//working now


return 0;
}