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.