I'm doing a project on data management using indexes. I am able to initially create,
read, & write Index. but when updating Index after a data record addition or deletion, I
encounter problems. When the Index is written back to file for update, it is written
incorrectly. I've narrowed down the problem to these code, I think.
Additions:
Code:
//new record goes under location found by binary srch
//shift index for additional entry
//addkey is global var
void indexinsert(const int& location, Index dindex[], int& size, const int& trrn){
int i;
dindex = (Index*) realloc (dindex, (size+1) * sizeof(Index));
if (dindex==NULL) exit (1);
for(i = size; i > location; i--){
dindex[i] = dindex[i-1];
}
strcpy(dindex[location].key,addkey);
dindex[location].rrn = trrn;
size++;
return;
}
below is the "tail" of the Index file. the new index entry is placed at end & is junk,
not what I intended. can't figure what went wrong.
N583FE|12
N711AV|23
N8500R|15
N87BC |1
N97890|25
TC-THG|13
|538976288
Deletions:
Code:
//data record of index entry @ location deleted
//shift index for update
void indexdelete(const int& location, Index dindex[], int& size){
int i;
for(i = location; i < (size-1); i++)
dindex[i] = dindex[i+1];
size--;
return;
}
deletion of 1 data record ok, but a second record deletion results in this index below.
index is updated properly, but there is extra junk at end of file (last line). I think
it is written to file by my code rather than left over by the file, as I've closed the
index file from reading & reopened it for writing with fopen mode "wb" to clear out
previous file contents.
N51126|24
N5638J|10
N583FE|12
N711AV|23
N8500R|15
N87BC |1
N97890|25
TC-THG|13
|0
for more reference, here's the index file output code:
Code:
void finalindex(Index data[], const int& size, FILE* dfile){
//write each entry of sorted index to file.
int i;
for(i = 0; i < size; i++){
fputs(data[i].key,dfile);
fputs("|",dfile);
fprintf(dfile,"%d",data[i].rrn);
fputc(10,dfile);
}
return;
}
Or is problem elsewhere?