Thread: Reading File

  1. #1
    Registered User marrk's Avatar
    Join Date
    Sep 2006
    Posts
    23

    Reading File

    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
    Last edited by marrk; 01-15-2007 at 02:23 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM