| | |
Insertion sort with files
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
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.
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).
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.
c Syntax (Toggle Plain Text)
void insertion_sort(int category) { FILE *seeker; int size=0, total=0, runner=0; int k; int i; ADMISSION_DATA small = {NULL,NULL,"","","",0,999,0.00,1,0}; ADMISSION_DATA temp = {NULL,NULL,"","","",0,999,0.00,1,0}; if( (seeker = fopen("guest_history.txt", "rb+")) == NULL ) { printf("\nGuest history file could not be opened"); _getch(); } else { // counts the number of cancellations in the file while ( fread( §ion, sizeof (ADMISSION_DATA), 1, seeker ) == 1 ) { total++; if( section.cancel == category ) size++; } rewind( seeker ); //printf("Count: %d", size); _getch(); size = 0; while ( fread( §ion, sizeof (ADMISSION_DATA), 1, seeker ) == 1 ) { if( section.cancel == category ) { for (k = 1; k < size; k++) { // sets the first row as the smallest //fseek( seeker, ( section.reservation_num - k ) * sizeof( ADMISSION_DATA ), SEEK_SET ); fread( &small, sizeof (ADMISSION_DATA), 1, seeker ); //rewind( seeker ); for(i = k - 1; i>=0 && (strcmp(small.first_name,section.first_name) < 0); i--) { //fseek( seeker, ( section.reservation_num - i ) * sizeof( ADMISSION_DATA ), SEEK_SET ); //fread( §ion, sizeof( ADMISSION_DATA ), 1, seeker ); temp = section; fseek( seeker, ( section.reservation_num ) * sizeof( ADMISSION_DATA ), SEEK_SET ); fwrite( &temp, sizeof( ADMISSION_DATA ), 1, seeker ); } //fseek( seeker, ( section.reservation_num ) * sizeof( ADMISSION_DATA ), SEEK_SET ); //fread( &small, sizeof( ADMISSION_DATA ), 1, seeker ); temp = small; fseek( seeker, ( section.reservation_num ) * sizeof( ADMISSION_DATA ), SEEK_SET ); fwrite( &small, sizeof( ADMISSION_DATA ), 1, seeker ); } } runner++; } } fclose( seeker ); //printf("RUN %d", runner); }
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.
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
Clicks the report function once
Clicks the report function a second time
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?
Unsorted Data
•
•
•
•
Alice
Zebra
Cat
Apple
•
•
•
•
Cat
Apple
Alice
Zebra
Clicks the report function a second time
•
•
•
•
Alice
Apple
Cat
Zebra
c Syntax (Toggle Plain Text)
/* +++++++++INSERTION SORT FUNCTION +++++++++++ */ void insertion_sort() { FILE *guest_Ptr; int size=0; int outer_run; int inner_run; ADMISSION_DATA small = {NULL,NULL,"","","",0,999,0.00,1,0}; if( (guest_Ptr = fopen("guest_history.txt", "rb+")) == NULL ) { textcolor(12); cprintf("\nERROR!! HISTORY FILE COULD NOT BE OPENED!"); sleep(3); } else { // counts the number of guest records in the file while ( fread( §ion, sizeof (ADMISSION_DATA), 1, guest_Ptr ) == 1 ) { size++; } rewind( guest_Ptr ); //printf("Count: %d", size); _getch(); for(outer_run = 1; outer_run < size; outer_run++) { fseek( guest_Ptr, ( outer_run ) * sizeof( ADMISSION_DATA ), SEEK_SET ); fread( §ion, sizeof( ADMISSION_DATA ), 1, guest_Ptr ); small = section; rewind ( guest_Ptr ); inner_run = outer_run - 1; fseek( guest_Ptr, ( inner_run ) * sizeof( ADMISSION_DATA ), SEEK_SET ); fread( §ion, sizeof( ADMISSION_DATA ), 1, guest_Ptr ); rewind ( guest_Ptr ); for(; inner_run >= 0 && (strcmp( small.first_name, section.first_name) <= 0 ); inner_run--) { fseek( guest_Ptr, ( inner_run ) * sizeof( ADMISSION_DATA ), SEEK_SET ); fread( §ion, sizeof( ADMISSION_DATA ), 1, guest_Ptr ); fseek( guest_Ptr, ( inner_run + 1 ) * sizeof( ADMISSION_DATA ), SEEK_SET ); fwrite( §ion, sizeof( ADMISSION_DATA ), 1, guest_Ptr ); } fseek( guest_Ptr, ( inner_run + 1 ) * sizeof( ADMISSION_DATA ), SEEK_SET ); fwrite( &small, sizeof( ADMISSION_DATA ), 1, guest_Ptr ); } } fclose( guest_Ptr ); } // Control is returned to the report function
![]() |
Similar Threads
Other Threads in the C Forum
- Previous Thread: need C program help
- Next Thread: need help with my c program just need to know how to fix those errors
Views: 1104 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays asterisks binarysearch calculate centimeter char command convert copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax directory dynamic executable fflush file fork forloop frequency getlasterror givemetehcodez graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop kernel km lazy linked linkedlist linux linuxsegmentationfault list lists locate logical_drives match matrix microsoft motherboard multi mysql number open opendocumentformat opensource owf pattern pdf performance pointer pointers posix problem probleminc program programming radix recursion recv repetition research scanf scheduling scripting segmentationfault send sequential shape socketprograming spoonfeeding stack standard string strings structures student systemcall testautomation turboc unix user variable voidmain() wab win32 windows.h






