Need to open a file and prompt user which field they want to sort on

Posted on 2004-10-25
Last Modified: 2010-04-15
I have to open a .dat file which contains the following strings: book title(max length 50), isbn(max length 15), and author(max length 30). From this file, I need to ask my user which field to sort on and then print the results. I don't understand how I can do this?
Question by:elmo68
    LVL 5

    Accepted Solution

    here are the bare bones

    int main()
    char title[100][50], isbn[100][15], author[100][30];
    FILE *fp = fopen("filname.dat", "r");
    int i = 0;
    int order = 0;
                           fscanf("%s %s %s\n", title[i], isbn[i], author[i]);

                  printf("please enter the order  u want to sort in: ");   // 1 for title, 2 for isbn, 3 for author
                   scanf("%d", &order);
                    case 1:
                   case 2:
                     case 3:
                                 printf("wrong order .. exiting \n");

                   // print in whatever order u sorted
    } else {
                   printf("failed to open the file\n");
    return 0;
    LVL 5

    Expert Comment

    case 1:
             sort_by_title(title[][50]);              // correction

    you will need to  write the sorting routine, most probably an alphabetical sort, is what you need
    LVL 8

    Expert Comment

    Why don't you start writing the code?
    We can help you after that.
    Don't expect the experts to write code for you from scratch. You will not learn anything, if we do that!
    So, my sincere suggestion for you is to start with code. Anyhow, van_dy has given the framework for you to work on.
    And come back when you get into problem while coding....

    LVL 12

    Expert Comment

    Hi elmo68,
    I smell homework...

    Think about how to generalize the sorting. This obviously has to do something with offset and length of each entry. Store this information in a single structure.



    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
    Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode ( They will have you believe that Unicode requires you to use…
    The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

    933 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now