Insertion sort with files

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Feb 2008
Posts: 88
Reputation: knight fyre is an unknown quantity at this point 
Solved Threads: 1
knight fyre's Avatar
knight fyre knight fyre is offline Offline
Junior Poster in Training

Insertion sort with files

 
0
  #1
Apr 5th, 2008
I'm trying to create a function that will sort the contents of a file in alphabetical order. The problem I'm having is that the code doesn't seem to do anything. The only examples I could find have to do with arrays which I am not using but I tried to apply the same principle to structs and files. The file I need to sort is a log file and the size of it's contents will be forever increasing. Here's the ineffective code:

NB: Basically I'm trying to display a report sorted alphabetically. If there's a simple way that doesn't involve modifying the file that would be great.

  1. void insertion_sort(int category)
  2. {
  3. FILE *seeker;
  4. int size=0, total=0, runner=0;
  5. int k;
  6. int i;
  7. ADMISSION_DATA small = {NULL,NULL,"","","",0,999,0.00,1,0};
  8. ADMISSION_DATA temp = {NULL,NULL,"","","",0,999,0.00,1,0};
  9.  
  10. if( (seeker = fopen("guest_history.txt", "rb+")) == NULL )
  11. {
  12. printf("\nGuest history file could not be opened");
  13. _getch();
  14. }
  15. else
  16. {
  17. // counts the number of cancellations in the file
  18. while ( fread( &section, sizeof (ADMISSION_DATA), 1, seeker ) == 1 )
  19. {
  20. total++;
  21. if( section.cancel == category )
  22. size++;
  23.  
  24. }
  25. rewind( seeker );
  26. //printf("Count: %d", size); _getch(); size = 0;
  27.  
  28. while ( fread( &section, sizeof (ADMISSION_DATA), 1, seeker ) == 1 )
  29. {
  30. if( section.cancel == category )
  31. {
  32. for (k = 1; k < size; k++)
  33. {
  34. // sets the first row as the smallest
  35. //fseek( seeker, ( section.reservation_num - k ) * sizeof( ADMISSION_DATA ), SEEK_SET );
  36. fread( &small, sizeof (ADMISSION_DATA), 1, seeker );
  37. //rewind( seeker );
  38.  
  39. for(i = k - 1; i>=0 && (strcmp(small.first_name,section.first_name) < 0); i--)
  40. {
  41. //fseek( seeker, ( section.reservation_num - i ) * sizeof( ADMISSION_DATA ), SEEK_SET );
  42. //fread( &section, sizeof( ADMISSION_DATA ), 1, seeker );
  43. temp = section;
  44. fseek( seeker, ( section.reservation_num ) * sizeof( ADMISSION_DATA ), SEEK_SET );
  45. fwrite( &temp, sizeof( ADMISSION_DATA ), 1, seeker );
  46. }
  47.  
  48. //fseek( seeker, ( section.reservation_num ) * sizeof( ADMISSION_DATA ), SEEK_SET );
  49. //fread( &small, sizeof( ADMISSION_DATA ), 1, seeker );
  50. temp = small;
  51. fseek( seeker, ( section.reservation_num ) * sizeof( ADMISSION_DATA ), SEEK_SET );
  52. fwrite( &small, sizeof( ADMISSION_DATA ), 1, seeker );
  53. }
  54. }
  55. runner++;
  56. }
  57. }
  58. fclose( seeker );
  59. //printf("RUN %d", runner);
  60. }

I doubt I can use the reservation number to navigate through the files because more than one row can have the same number (1 - 50).
Last edited by knight fyre; Apr 5th, 2008 at 7:46 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Insertion sort with files

 
0
  #2
Apr 6th, 2008
Between every fwrite() and fread() call, there should be a fflush() call.

I hope this is limited to a small number of unsorted records at the end of the file, because this approach is going to take some time.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 88
Reputation: knight fyre is an unknown quantity at this point 
Solved Threads: 1
knight fyre's Avatar
knight fyre knight fyre is offline Offline
Junior Poster in Training

Re: Insertion sort with files

 
0
  #3
Apr 6th, 2008
I recoded my insertion sort function. With almost a full day between my first post it now works but it doesn't work perfectly. The issue is that the sorting function will partially sort the guest names and display the report. For example, if I have


Unsorted Data
Alice
Zebra
Cat
Apple
Clicks the report function once
Cat
Apple
Alice
Zebra

Clicks the report function a second time

Alice
Apple
Cat
Zebra
Why isn't the data being sorted correctly sorted the first time the report is displayed and how do I get it only display when the list is fully sorted?

  1. /* +++++++++INSERTION SORT FUNCTION +++++++++++ */
  2. void insertion_sort()
  3. {
  4. FILE *guest_Ptr;
  5. int size=0;
  6. int outer_run;
  7. int inner_run;
  8.  
  9. ADMISSION_DATA small = {NULL,NULL,"","","",0,999,0.00,1,0};
  10.  
  11.  
  12. if( (guest_Ptr = fopen("guest_history.txt", "rb+")) == NULL )
  13. {
  14. textcolor(12); cprintf("\nERROR!! HISTORY FILE COULD NOT BE OPENED!"); sleep(3);
  15. }
  16. else
  17. {
  18. // counts the number of guest records in the file
  19. while ( fread( &section, sizeof (ADMISSION_DATA), 1, guest_Ptr ) == 1 )
  20. {
  21. size++;
  22. }
  23.  
  24. rewind( guest_Ptr );
  25. //printf("Count: %d", size); _getch();
  26.  
  27. for(outer_run = 1; outer_run < size; outer_run++)
  28. {
  29. fseek( guest_Ptr, ( outer_run ) * sizeof( ADMISSION_DATA ), SEEK_SET );
  30. fread( &section, sizeof( ADMISSION_DATA ), 1, guest_Ptr );
  31. small = section;
  32.  
  33. rewind ( guest_Ptr );
  34.  
  35. inner_run = outer_run - 1;
  36.  
  37. fseek( guest_Ptr, ( inner_run ) * sizeof( ADMISSION_DATA ), SEEK_SET );
  38. fread( &section, sizeof( ADMISSION_DATA ), 1, guest_Ptr );
  39. rewind ( guest_Ptr );
  40.  
  41. for(; inner_run >= 0 && (strcmp( small.first_name, section.first_name) <= 0 ); inner_run--)
  42. {
  43. fseek( guest_Ptr, ( inner_run ) * sizeof( ADMISSION_DATA ), SEEK_SET );
  44. fread( &section, sizeof( ADMISSION_DATA ), 1, guest_Ptr );
  45.  
  46. fseek( guest_Ptr, ( inner_run + 1 ) * sizeof( ADMISSION_DATA ), SEEK_SET );
  47. fwrite( &section, sizeof( ADMISSION_DATA ), 1, guest_Ptr );
  48. }
  49.  
  50. fseek( guest_Ptr, ( inner_run + 1 ) * sizeof( ADMISSION_DATA ), SEEK_SET );
  51. fwrite( &small, sizeof( ADMISSION_DATA ), 1, guest_Ptr );
  52. }
  53. }
  54. fclose( guest_Ptr );
  55. } // Control is returned to the report function
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 88
Reputation: knight fyre is an unknown quantity at this point 
Solved Threads: 1
knight fyre's Avatar
knight fyre knight fyre is offline Offline
Junior Poster in Training

Re: Insertion sort with files

 
0
  #4
Apr 6th, 2008
No worries, I solved the problem.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 1104 | Replies: 3
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC