Solved

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

Posted on 2004-10-25
167 Views
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?
0
Question by:elmo68
    4 Comments
     
    LVL 5

    Accepted Solution

    by:
    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;
    if(fp){
                 while(feof(fp)){
                           fscanf("%s %s %s\n", title[i], isbn[i], author[i]);
                           i++;
                 }

                  printf("please enter the order  u want to sort in: ");   // 1 for title, 2 for isbn, 3 for author
                   scanf("%d", &order);
                   switch(order){
                    case 1:
                           sort_by_title(tite[50]);
                            break;
                   case 2:
                             sort_by_isbn(isbn[][15]);
                             break;
                     case 3:
                               sort_by_auth(author[][30]);
                               break;
                      default:
                                 printf("wrong order .. exiting \n");
                                  exit(1);
                       }


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

    Expert Comment

    by:van_dy
    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
    0
     
    LVL 8

    Expert Comment

    by:ssnkumar
    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....

    -ssnkumar
    0
     
    LVL 12

    Expert Comment

    by:stefan73
    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.

    Cheers!

    Stefan
    0

    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 (http://en.wikipedia.org/wiki/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