I am having a problem when trying to delete specific record from a text file.
Data structure is as follows:

typedef struct
{
    char id[9];
    char name[50];
    float amount,price;
}PRODUCT;

typedef struct id
{
   char key[9];
   int address;
}ID;//for searching

typedef struct node
{
    ID id;
    struct node *left,*right;
}NODE;

Function for seaching by id:

NODE* search(NODE* root,char *key)
{
  if (root == 0)
    return 0;
  else if (strcmp(key,root->id.key) == 0)
    return root;
  else if (strcmp(key,root->id.key) > 0)
    return search(root->left,key);
  else
    return search(root->right,key);
return root;    
}

Part of main() for removing specific record:

      int main()
      { //...//
      PRODUCT prod;
      NODE *searching_result=0;
      printf(" Enter ID for searching: ");
      scanf("%s",prod.id);
      searching_result=searching(root,prod.id);
      if(searching_result == 0)
        printf(" Nothing found.");
        else{
        FILE *fptr1=fopen("products.txt","r");
        FILE *fptr2=fopen("temp.txt","a");
        rewind(fptr1);
        while(!feof(fptr1))
        {
            fscanf(fptr1,"%s %s %f %f\n",
                  prod.id,prod.name,&prod.amount,&prod.price);
            if(!strcmp(prod.id,id.key))
                                     fprintf(fptr2,"%s %-20s %6.2f %6.2f\n",
                          prod.id,prod.name,prod.amount,prod.price);
        }
        fclose(fptr1);
        fclose(fptr2);
        remove("products.txt");
        rename("temp.txt","products.txt");
        }}
        //...//

The code is too long to post, so could someone just point out what is wrong with the main() part?

Thanks for replies.

Recommended Answers

All 5 Replies

It looks like you started with the concept of a linked list with left and right then in your delete decided to serially read in the file but skip over the item to be deleted and finally rename the files.

That would fundamentally break a linked list and is not so much a coding error but a design error.

Time to redesign? If you want to go with linked lists (right and left?) then a routine would be need to rebuild the links (left and right?)
https://www.cs.bu.edu/teaching/c/linked-list/delete/

@rproffitt,
Actually, data structure is binary search tree where left and right represents left and right branches.

@negru, so it does appear your code hacks out items but doesn't rebuild the tree. Sounds like more work to be done.
Keep in mind there are so many articles on removing items from search trees that I didn't duplicate that here. That is, you need to reconsider the design if it looks as I see it.

@rproffitt,
I added the functions for removing node from a BST, inorder traversion, changed the part of main() for removing specific record.
But I still can't figure out why it is not working. Could you please check the following code:

NODE *remove_BST_node(NODE *root,char id[9])
{
      if(root == 0)
        return 0;
   else if(strcmp(id,root->id.key) < 0)
     root->left=remove_BST_node(root->left,id);
   else if(strcmp(strcmp(id,root->id.key) > 0)
    root->right=remove_BST_node(root->right,id);
   else if(root->left == 0)
   {
     NODE *p=root->right;
     free(root);
     return p;
   }
   else if(root->right == 0)
   {
     NODE *p=root->left;
     free(root);
     return p;
   }
   else
   {
      NODE *p=search_largest(root->left);
      root->id=p->id;
      root->right=remove_BST_node(root->left,root->id.key);
   }
   return root; 
}

NODE *search_largest(NODE *root)//search in the right branch
{
   while(root->right != 0)
      root->right=root;
   return root;
}

void print_inorder(NODE *root)//inorder traversion
{
    FILE *fptr1;
    PRODUCT prod;
    if(root != 0)
    {
      print_inorder(root->left);
  fprintf(fptr1,"%05d %-20s %6.2f %6.2f\n",
          atoi(prod.id), prod.name, prod.amount, prod.price);
    }
}

int main()
{
      //...//
      NODE *searching_result=0;
      PRODUCT prod;
      NODE *root=0;
      FILE *fptr1;
            int j;
            fptr1=fopen("products.txt","r");
            for(j=0;j<n;j++)//read data from file
                fscanf(fptr1,"%s %s %f %f\n",prod.id,prod.name,&prod.amount,&prod.price);
            fclose(fptr1);
            fptr1=fopen("products.txt","w");
            printf("enter product with ID to be removed");
            scanf("%s",prod.id);
            searching_result=searching(root,prod.id);
            if(searching_result == 0)
              printf("Nothing found.");
            else{  
            root=remove_BST_node(root,prod.id);//remove node
            print_inorder(root);//print after removing (overwrite in file)
            fclose(fptr1);
            }
         }
    //...//  
}

@negru. "it is not working" means I would have to work very hard. Sorry but you'll have to do that work for me. You can add printf statements aroung the code you suspect or just single step in your IDE.

My overall observation is the design looks like one from decades ago. That is, no one I know would make a database like that. Why not use some SQLite or such? Free stuff without the headaches you are creating.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.