Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2004-10-25
4
Medium Priority
?
171 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
Comment
Question by:elmo68
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 5

Accepted Solution

by:
van_dy earned 2000 total points
ID: 12405433
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
ID: 12405445
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
ID: 12407777
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
ID: 12410219
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Suggested Courses

610 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