This is quite a complex problem (at least for me) so bare with me while I explain it.
The initial problem is to read from a student database, search for the type of student (e.g. European, International, Funded etc.) and sort alphabetically by name.
To begin with I read from each individual file line by line using fgets() and parsed each line using sscanf() to store the students surname and first name in said variables.
Finally I used fprintf() to write each file to one single file which contains all the names of students in tabular format.
Easy enough so far.
Now comes the complicated part.
To save all the names from the combined file I used a 2D char array. It's 2D because I need to store each ROW number and #define BUFFER to specify how many chars in a line.
Now I need to pass the 2D array to a function to search for a student and sort the list. I have given it a try but I can only get it to display one name or random characters.
Below is snippets of my code so far...
What I have tried:
void parse_file_and_sort(void)
{
FILE *fp = fopen("Combined.dat", "rb");
static char student[SIZE][BUFFER];
unsigned short total;
unsigned short i = 0;
char input;
char search_key[KEY_ID];
if (fp != NULL)
{
while((fgets(student[i], BUFFER, fp)) != NULL)
{
if (student[i][strlen(student[i] - 1)] == '\n')
{
student[i][(strlen(student[i]) - 1)] = '\0';
i++;
}
}
total = i;
for(i = 0; i < total; i++)
{
if (i == 0)
{
student[i][i] = '\0';
}
}
fclose(fp);
linear_search(student);
}
void linear_search(char student[][BUFFER])
{
unsigned short i;
for(i = 0; i < SIZE; i++)
{
printf("%-2u | %s\n", i, student[i]);
}
}