I have a structure that is like:
typedef struct Student {
char name[50];
int roll_no;
char telephone[50];
char address[50];
}
Student;
And an array of such structures,
typedef struct SReg {
Student arr[MAX_STUDENTS];
int curr_length; //curr_length stores number of students currently in the student register, MAX is 100
}
SReg;
I have an SReg variable sr.
The name field contains names in Surname, Firstname format. For example, Rogers, Steve. I want to be able to sort the structures dumped into a binary file by their firstnames. The contents of sr has been exported to the binary file as follows.
FILE *file = fopen(fname, "wb");
if (file == NULL) {
printf("The file cannot be created!");
return 0;
}
if (fwrite(sr, sizeof(Student), sr->curr_length, file) != sr->curr_length) return 0;
fclose(file);
return 1;
}
Now I want to implement bubble sort based on the firstnames of the "name" field of the structure. I can get to the first names of students once I can access the name field of the structure using:
char *ptr_i = NULL;
char *ptr_j = NULL;
// To skip surname and get to first name
ptr_i = strchr(sr->arr[i].name, ' ') + 1;
ptr_j = strchr(sr->arr[j].name, ' ') + 1;
// ptr_i and ptr_j contain the two firstnames to be compared, and compare using strcmp
if (strcmp(ptr_i, ptr_j) > 0) {
Student temp;
temp = sr->arr[i];
sr->arr[i] = sr->arr[j];
sr->arr[j] = temp;
}
But my question is, how do I access the name field of each structure stored in the binary file? I can get the total number of such structures in the file by
fseek(file, 0, SEEK_END);
// get total number of records in file
int total = ftell(file)/sizeof(Student);
rewind(file);
So after that I can use two nested for loops and make them run total number of iterations and get a bubble sort algo. But my question is, how do I actually access the names and compare them? How can I get to the first names? I'm doing this because I need to sort a binary file in place. I've seen that this can probably done using fseek, fwrite and fread, but no clue actually how.
fread, then sort the array and write it back. With a binary file it's generally better if you write a small header to the file including at least a version number that you'd increment if the struct ever changed and the number of entries. I like to add a 4 byte identifier too so I can tell if the file is indeed the type I expect. Since you don't have that you probably want to be sure the file length % sizeof(Student) is 0.Surname, Firstnamegiven the two fields. It's hard to print just the surname, or just the first name, if you store them combined.mmapwill make life much easier.qsort()it like any other in-memory array.