• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 174
  • Last Modified:

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

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
elmo68
Asked:
elmo68
  • 2
1 Solution
 
van_dyCommented:
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
 
van_dyCommented:
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
 
ssnkumarCommented:
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
 
stefan73Commented:
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

Live webcast with Pinal Dave

Pinal Dave will teach you tricks to help identify the real root cause of database problems rather than red herrings. Attendees will learn scripts that they can use in their environment to immediately figure out their performance Blame Shifters and fix them quickly.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now