Hi guys
I saw faq - http://faq.cprogramming.com/cgi-bin/...&id=1043284351 and I noticed that i'm doing exactly the same thing with feof(), now I'm wondering if this is the right way to read file:
Code:
void LoadDB(goods **root)
{
int j = 0;
int q = 0;
int keep_on = 1;
char choice;
char file_name[20];
goods *product;
goods *search;
goods *last;
goods *lastp;
goods **prev;
FILE *k;
clrscr();
printf("LOAD\n\n");
printf("Open file: ");
GetStr(file_name, 15);
for(j = 0; j < 17; j++)
{
if(file_name[j] == 0)
{
file_name[j] = '.';
file_name[j+1] = 't';
file_name[j+2] = 'x';
file_name[j+3] = 't';
file_name[j+4] = 0;
break;
}
}
k = fopen(file_name, "r");
if(k == NULL)
{
_setcursortype(_NOCURSOR);
clrscr();
printf("File %s could not be opened!", file_name);
getch();
}
else
{
clrscr();
//with THIS
while(keep_on == 1)
{
search = *root;
prev = root;
product = NULL;
product = malloc(sizeof(struct goods));
if(product != NULL)
{
//and THIS I'm controling the loop
if(fscanf(k, "%16s\t%d\t%f\t%d\n", product->product_name, &product->code, &product->price, &product->supply) == 4)
{
product->left = NULL;
product->right = NULL;
keep_on = 1;
for(q = 0; product->product_name[q] != 0; q++)
{
if(product->product_name[q] == '_')
{
product->product_name[q] = ' ';
}
}
//check if there are any duplicates
while(search != NULL)
{
if(search->code == product->code)
{
printf("-------------------------------------------------------------\n");
printf("Code %d already exist.\n", search->code);
printf("Current product: %s, %d, %.2f, %d\n", search->product_name, search->code, search->price, search->supply);
printf("Product from file (%s): %s, %d, %.2f, %d\n", file_name, product->product_name, product->code, product->price, product->supply);
printf("Replace current product?(Y/N)\n");
choice = getch();
//replace product with the new one
if(choice == 'Y' || choice == 'y')
{
//delete current product
//product has no right child
if(search->right == NULL )
{
*prev = search->left;
free(search);
break;
}
//product has a right child but it's right child has no left child
else if(search->right->left == NULL)
{
search->right->left = search->left;
*prev = search->right;
free(search);
break;
}
//node has a right child and it's right child has a left child
else
{
//swap deleting node with a smallest value from a right subtree
last = search->right;
while(last->left != NULL)
{
lastp = last;
last = last->left;
}
lastp->left = last->right;
last->left = search->left;
last->right = search->right;
*prev = last;
free(search);
break;
}
}
//do not replace current product
else
{
free(product);
goto next_prod;
}
}
else if(search->code < product->code)
{
prev = &search->right;
search = search->right;
}
else if(search->code > product->code)
{
prev = &search->left;
search = search->left;
}
}
//insert current product
prev = root;
search = *root;
while(search != NULL)
{
if(search->code < product->code)
{
prev = &search->right;
search = search->right;
}
else if(search->code > product->code)
{
prev = &search->left;
search = search->left;
}
}
*prev = product;
}
else
{
keep_on = 0;
}
}
else
{
printf("Out of memory!");
getch();
goto out;
}
next_prod:;
}
_setcursortype(_NOCURSOR);
clrscr();
printf("File %s successfully loaded!", file_name);
getch();
out:;
z = 0;
fclose(k);
}
_setcursortype(_NORMALCURSOR);
}
I'm reading datas into a structure(binary tree).
sorry if code is to long