Thread: sort struct from file help!

  1. #1
    Registered User xxxrugby's Avatar
    Join Date
    Jan 2005
    Posts
    178

    sort struct from file help!

    Need help.
    I read all about qsort, and tried qsort and lots of my ideas but I just can't get alphabet order.
    Maybe I need to make some changes in program, or linked struct or something. open for all ideas. This program works me at home.

    Can you help me in finishing function -- void list_records(FILE*);
    -- case '2'.
    Sorting struct from file in alphabet order. Does not need to be in the same file. No problem for creating another file or something.

    in advance:
    Thank's for your time and help.

    Code:
    #include <stdio.h>
    #include <string.h>
    
     struct book {
           char name[30];
           char publisher[30];
           int year;
    }data;
    
    void add_records(FILE*);
    void list_records(FILE*);
    void modify_records(FILE*);
    
    int main()
    {
     FILE *t, *p;  int choice; int choice_search; char booknames[30];
    
     if((t = fopen("BOOK.DAT","rb+"))==NULL)
     {
           if((t = fopen("BOOK.DAT","wb+"))==NULL)
           {
                 printf("The File can't be open\n");
                 exit(1);
           }
     }
    
     system("cls");
    
     printf("                  BOOK DIRECTORY                      \n");
     printf("          ******************************              \n");
    
     printf("1 - Add Records\n"
            "2 - List Records\n"
            "3 - Modify Records\n"
            "4 - Delite Records\n"
            "0 - Exit\n"
            "Your Chice:\n");
     fflush(stdin);
     choice = getche();
    
     switch(choice)
     {
        case '1': add_records(t); break;
        case '2': list_records(t); break;
        case '3': modify_records(t); break;
        case '4':
            printf("\nEnter name of book to delete:\n");
    	    scanf("%s", booknames);
            p = fopen("TEMP.DAT","wb");
            rewind(t);
            while(fread( &data, sizeof(data), 1, t) ==1 )
    	    {
    	        if(strcmp(data.name,booknames)!=0)
                fwrite( &data, recsize, 1, p);
    	    }
            fclose(t); fclose(p);
            remove("BOOK.DAT"); rename("TEMP.DAT","BOOK.DAT");
    
             main();
         break;
        case '0': printf("\n"); fclose(t); exit(1);
     }
    
     fclose(t);
     printf("\n\nError! Unknown Choice!\n\n");
     main();
     return 0;
    }
    
    void add_records(FILE*t)
    {
     char another;
     fseek(t, 0, SEEK_END);
     another = 'y';
     while(another == 'y')
     {
        printf("\nEnter Book name, Publisher and Year of publishing\n");
        gets(data.name); gets(data.publisher); scanf("%d",&data.year);
        fwrite(&data, sizeof(data),1,t);
    
        printf("Add another data (y/n):");
        fflush(stdin);
        another = getche();
        printf("\n");
     } fclose(t);
     printf("\n");
     main();
    }
    
    void list_records(FILE*t)
    {
     int listc;  int i; int n = 0;
     rewind(t);
     printf("\nListanje po:\n"
            "1 - Time Record\n"
            "2 - Alphabet order\n");
     fflush(stdin);
     listc = getche();
     switch(listc)
     {
        case '1':
            printf("\n");
            printf("---------------------------------------------------------------------\n");
            printf("|           BOOK               |          PUBLISHER           | YEAR |\n");
            printf("---------------------------------------------------------------------\n");
            rewind(t);
            while(fread( &data, sizeof(data), 1, t) == 1)
                printf("|%-30s|%-30s| %6d|\n", &data.name,&data.publisher,data.year);
            printf("------------------------------------------------------------------\n");
            system("PAUSE");
            printf("\n"); break;
        case '2':
            printf("\nNot finished at this moment\n"); break;
     } fclose(t); printf("\n");
     main();
    }
    
    void modify_records(FILE *t)
    {
     char another = 'y'; char bookname[30];
     while(another == 'y')
     {
        printf("\nEnter book name to modify:");
        gets(bookname);
        rewind(t);
        while(fread( &data, sizeof(data), 1, t ) == 1)
        {
            if(strcmp(data.name, bookname) == 0)
            {
                printf("\nEnter new name, publisher and year of publishing\n");
                gets(data.name); gets(data.publisher); scanf("%d", &data.year);
    
                fseek(t, -sizeof(data), SEEK_CUR);
                fwrite(&data, recsize, 1, t);
                break;
            }
        }
        printf("Modify another record (y/n):");
            fflush(stdin);
            another = getche();
        printf("\n");
     } fclose(t); printf("\n");
     main();
    }
    Last edited by xxxrugby; 01-09-2005 at 04:44 PM.

  2. #2
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    Thank you :)
    Last edited by Kleid-0; 01-09-2005 at 04:53 PM.

  3. #3
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    Do not use fflush(stdin);
    This is Why
    This is what you use other than fflush(stdin);

    Do not use getche(); it's not standard (lol; but seriously..)
    Evidence here
    Use getchar() instead of getche();

    Change int main() to int main(void)
    This is Why

    And about your main problem, I'm going to need a little more time lol :P I like your code by the way!

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> I read all about qsort, and tried qsort and lots of my ideas but I just can't get alphabet order.

    since you don't have any code posted for the sorting routine it's hard to say why it doesn't work.

    >> Maybe I need to make some changes in program, or linked struct or something.

    sounds like a good idea, that's how it's usually done - data is maintained in memory and flushed to file when changes are made.

    >> gets(data.name); gets(data.publisher); scanf("%d",&data.year);

    mixing gets with scanf is a bad idea (and gets is unsafe besides) - read the FAQ for more on all of that.

    // fflush(stdin)

    that results in undefined behaviour. also in the FAQ.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    There are a few problems with your code you need to fix before worrying about sorting the data in alphabetical order.

    - main() - don't call this within your code, leave that to the invoking operating system. When a function ends, control will return to the calling function, so you definately don't want to be calling a function at the ****_records() functions (like you currently are with main)

    fflush(stdin) is wrong. Read the FAQ:
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351

    gets() is a function to avoid, read this FAQ:
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351

    Now, in order to sort the data, you're going to need it all in memory. I'd suggest reading up on some list techniques, maybe this will help:
    http://faq.cprogramming.com/cgi-bin/...ect=1073086407

    [edit]
    I see I'm beaten, oh well!
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  2. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  3. Bi-Directional Linked Lists
    By Thantos in forum C Programming
    Replies: 6
    Last Post: 12-11-2003, 10:24 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM