sorting in a file!!

This is a discussion on sorting in a file!! within the C Programming forums, part of the General Programming Boards category; Hi, I've written two functions, one that fill up an array of structures and write it in a file, the ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    103

    sorting in a file!!

    Hi, I've written two functions, one that fill up an array of structures and write it in a file, the other one should sort the structures and put them in a file.

    Code:
    void fill_up_emp(employee y[], FILE *ptr){
                       while(!(ptr=fopen("employee.txt","w")))
                               printf("Cannot open file, Retry:\n");
                       for (int i = 0 ; i < MAX ; i++){
                       printf("First name: ");
                       scanf("%s",y[i].first_name);
                       fprintf(ptr,"%s\n",y[i].first_name);
                       printf("Last name: ");
                       scanf("%s",y[i].last_name);
                       fprintf(ptr,"%s\n",y[i].last_name);
                       printf("Salary: ");
                       scanf("%lf",&y[i].salary);
                       fprintf(ptr,"%lf",y[i].salary);
                       
                       }
    
                       fclose(ptr);
    void selection_sort_stu(student x[], FILE * ptr){
                             int result;
                             int min;
                             FILE *ptr_up;
                             while(!(ptr=fopen("student.txt","r")))
                               printf("Cannot open file, Retry:\n");
                             while(!(ptr=fopen("student_updated.txt","w")))
                               printf("Cannot open file, Retry:\n");  
                             for (int i = 0 ; i < MAX  - 1 ; i++){
                                 min = i;
                                 fscanf(ptr,"%s",x[i].first_name);
                                 for (int j = i + 1 ; j < MAX; j++){
                                     fscanf(ptr,"%s",x[j].first_name);
                                     if ((result = strcmp(x[i].first_name,x[j].first_name)) > 0)
                                                 min = j;
                                     else if (!(result = strcmp(x[i].first_name,x[j].first_name))){
                                             if ((result = strcmp(x[i].last_name,x[j].last_name))>0)
                                                min = j;
                                          }
                                     }
                                      fprintf(ptr_up,"%15s %15s %15.2lf\n",x[min].first_name,x[min].last_name,x[min].GPA);
                                      fprintf(ptr_up,"%15s %15s %15.2lf\n",x[i].first_name,x[i].last_name,x[i].GPA);
                                 }   
                                 fclose(ptr);
    What is wrong??

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Your indentation is wrong, for one...

    Code:
    void fill_up_emp(employee y[], FILE *ptr){
                       while(!(ptr=fopen("employee.txt","w")))
                               printf("Cannot open file, Retry:\n");
                       for (int i = 0 ; i < MAX ; i++){
                         printf("First name: ");
                         scanf("&#37;s",y[i].first_name);
                         fprintf(ptr,"%s\n",y[i].first_name);
                         printf("Last name: ");
                         scanf("%s",y[i].last_name);
                         fprintf(ptr,"%s\n",y[i].last_name);
                         printf("Salary: ");
                         scanf("%lf",&y[i].salary);
                         fprintf(ptr,"%lf",y[i].salary);
                       }
                       fclose(ptr);
    }
    void selection_sort_stu(student x[], FILE * ptr){
                             int result;
                             int min;
                             FILE *ptr_up;
                             while(!(ptr=fopen("student.txt","r")))
                               printf("Cannot open file, Retry:\n");
                             while(!(ptr=fopen("student_updated.txt","w")))
                               printf("Cannot open file, Retry:\n");  
                             for (int i = 0 ; i < MAX  - 1 ; i++){
                                 min = i;
                                 fscanf(ptr,"%s",x[i].first_name);
                                 for (int j = i + 1 ; j < MAX; j++){
                                     fscanf(ptr,"%s",x[j].first_name);
                                     if ((result = strcmp(x[i].first_name,x[j].first_name)) > 0)
                                                 min = j;
                                     else if (!(result = strcmp(x[i].first_name,x[j].first_name))){
                                             if ((result = strcmp(x[i].last_name,x[j].last_name))>0)
                                                min = j;
                                          }
                                     }
                                      fprintf(ptr_up,"%15s %15s %15.2lf\n",x[min].first_name,x[min].last_name,x[min].GPA);
                                      fprintf(ptr_up,"%15s %15s %15.2lf\n",x[i].first_name,x[i].last_name,x[i].GPA);
                                 }   
                                 fclose(ptr);
              }
    Wait... no closing brackets.
    Last edited by master5001; 09-24-2008 at 03:47 PM.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    103
    Hii!
    that was just a problem of "past"!! I understand that kind of problem when the compiler says so..
    Still there is a problem!! I don't know exactly why the data doesn't get ordered in the new file!!

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Well I am not going to lie, I didn't go line by line through your code since the indentation was giving me a headache. Plus this is not your entire program. So how do I know your problem is not elsewhere. In other words, did your compiler say there is something wrong here, or did you deduce there is? Either way, if this is the broken code, fix the indentation and I will look at it.

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I lied again.... I glanced at the code (as-is)

    So first off, here is a possible infinite loop.....
    Code:
    while(!(ptr=fopen("employee.txt","w"))) <-- why even have ptr as a parameter?
      printf("Cannot open file, Retry:\n");
    And while glancing at your sorting algorithm (If Malcolm sees this he will likely have a stroke) I am seriously wondering what on earth leads you to believe this will sort your data?

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    103
    Well
    I agree with you in all possible ways,
    First because i've tried to modify a previous code I have written to match the requirements of the actual assignment.
    I need a better Idea to retrieve the data from the file, sort it. and then rewrite it in the same file(which I didn't know how to do it), or to another file.
    so here is a funxtion that fills the array.
    Code:
    void fill_up_stu(student x[],FILE* ptr){
                       
                       while(!(ptr=fopen("student.txt","w")))
                               printf("Cannot open file, Retry:\n");
                       for (int i = 0 ; i < MAX ; i++){
                       printf("First name: ");
                       scanf("%s",x[i].first_name);
                       fprintf(ptr,"%s ",x[i].first_name);
                       printf("Last name: ");
                       scanf("%s",x[i].last_name);
                       fprintf(ptr,"%s\n",x[i].last_name);
                       printf("GPA (Less than or equal 4): ");
                       scanf("%lf",&x[i].GPA);
                       fprintf(ptr,"%.2lf\n",x[i].GPA);
                       }
                       fclose(ptr);
             }
    I don't know, but the identation seems to be different when a past if in the website.
    and now I need to sort that data in the file..what I'm sending there is a structure containing a firt name, last name and a GPA!!
    the code for sorting, it obviously the source of the problem since my program craches at this place.
    Code:
    void selection_sort_stu(student x[], FILE * ptr){
                             int result;
                             int min;
                             FILE *ptr_up;
                             if(!(ptr=fopen("student.txt","r"))){
                               printf("Cannot open file.\n");
                               exit(1);
                               }
                             if(!(ptr_up=fopen("student_updated.txt","w"))){
                               printf("Cannot open file.\n");  
                               exit(1);
                               }
                             for (int i = 0 ; i < MAX  - 1 ; i++){
                                 min = i;
                                 fscanf(ptr,"%s",x[i].first_name);
                                 for (int j = i + 1 ; j < MAX; j++){
                                     fscanf(ptr,"%s",x[j].first_name);
                                     if ((result = strcmp(x[i].first_name,x[j].first_name)) > 0)
                                                 min = j;
                                     else if (!(result = strcmp(x[i].first_name,x[j].first_name))){
                                             if ((result = strcmp(x[i].last_name,x[j].last_name))>0)
                                                min = j;
                                          }
                                     }
                                      fprintf(ptr_up,"%15s %15s %15.2lf\n",x[min].first_name,x[min].last_name,x[min].GPA);
                                      fprintf(ptr_up,"%15s %15s %15.2lf\n",x[i].first_name,x[i].last_name,x[i].GPA);
                                 }   
                                 fclose(ptr);
    Thanks again!

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Why not sort the data in the first place?

    But all things equal, lets start off with the fact that you are misusing fscanf(). You should actually just read in the data line-by-line in my opinion. Though to be frank, I don't think there is a performance loss by using fscanf(). It is just easier to post-process a whole line than to just assume the world of *scanf().

    Example:
    Code:
    char buffer_a[512], buffer_b[512];
    int x;
    float y;
    const char *literal = "hello 1 how're 2.0?"
    
    printf("&#37;d\n", sscanf(literal, "%s %d %s %f", buffer_a, &x, buffer_b, &y));

  8. #8
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193
    I'm sorry to interrupt on this post, but what's that line doing?
    Code:
    const char *literal = "hello 1 how're 2.0?"
    Can the pointers store data like that?

  9. #9
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Yes. That is actually the correct way to store a string literal.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by lautarox View Post
    I'm sorry to interrupt on this post, but what's that line doing?
    Code:
    const char *literal = "hello 1 how're 2.0?"
    Can the pointers store data like that?
    Of course the pointer doesn't store data. But the program stores data, somewhere, and if you put "hello 1 how're 2.0?" in your code somewhere the program is obligated to store it, somewhere. And you can assign a pointer to point to that piece of memory. (A string literal is really an array of const char, and an array used by itself decays to a pointer to its first element.)

  11. #11
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193
    So it would be like a readable string..
    That was a doubt I've had since I started coding and I saw that on a script..

  12. #12
    Registered User
    Join Date
    Apr 2008
    Posts
    103
    Guys, please keep in mind that what I wanna do is an extra, what we are asking is to assume the data is sorted..But I don't wanna be that lazy,
    Also I just started dealing with files 2 days ago, so please forgive my lack of knowledge and focus on my problem right now..
    That will be much appreciated.

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Leojeen View Post
    Guys, please keep in mind that what I wanna do is an extra, what we are asking is to assume the data is sorted..But I don't wanna be that lazy,
    Also I just started dealing with files 2 days ago, so please forgive my lack of knowledge and focus on my problem right now..
    That will be much appreciated.
    We're not focusing much on your question because no one seems to know what your question is. It's crashing, in that one of your error messages are printing? You're getting a segfault? How far do you get in your code? (I.e, have you put print statements through your function to see where it gets to?)

    One thing that could be a problem is if your first_name and last_name fields are char* instead of char[], because then you don't have any room to read them into out of the file. Also your selection sort seems to read way too many first_names and nothing else.

  14. #14
    Registered User
    Join Date
    Apr 2008
    Posts
    103
    Ok, thanks: I see the problem now..thanks, it getting late so I may probably work on it tomorrow and keep you updated.
    Good night.

  15. #15
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,302

    Unhappy Eeeek!

    I think your biggest problem with the sorting is that you are trying to combine 3 things that you simply cant combine:
    1. Reading the data in
    2. Sorting the data
    3. Writing the data out

    You have to do those things all seperately, or at the very least seperate those first two, otherwise it can't actually work. Think about what happens with your logic at the moment (forget for the moment that it isn't even close to working) when the last item you read in needs to be the first one in the output file. That should give you an immediate hint as to why you must read in the entire file first, and then sort it, before you write it out.
    That said, you'd need to come up with a way of storing the entire contents in memory, prior to and during sorting. Also, that method of storing the data needs to allow the items to be swapped around.
    Once you can store it in memory, and you can swap any two items at will, then you can sort it.

    The only other option is to perform an external sort which basically means that you produce intermediate files that are in some way closer to being sorted, and then read those in and produce something fully sorted (or at least even closer). Unless the file is very large, for example 1GB then you don't want to do that!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  2. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21