file index update error

This is a discussion on file index update error within the C Programming forums, part of the General Programming Boards category; I'm doing a project on data management using indexes. I am able to initially create, read, & write Index. but ...

  1. #1
    Registered User daluu's Avatar
    Join Date
    Dec 2002
    Posts
    42

    file index update error

    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?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    void indexinsert(const int& location, Index dindex[], int& size, const int& trrn
    This is not valid C code. This is C++. You are trying to use references. There are no references in C. Use pointers.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 03:35 PM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  4. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM
  5. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21