I have a question about sorting records in a file. This is my code
case 7: {
struct record ar[1000];
int x=-1;
if((ftemp = fopen(file_name, "r")) != NULL) {
if((f = fopen(temp, "w")) != NULL) {
while(fgets(line, 100, ftemp) != NULL) {
x++;
ar[x] = parse_line(line); //parse the line and store in an array
}
}
}
printf("What do you want sort?\n\n1 - region\n2 - area\n3 - population\nYour choose: ");
while(scanf("%d%c", &choose_sort, &symbol) != 2 || symbol != '\n' || choose_sort > 3 || choose_sort < 1) {
printf("\nInvalid input. Please, try again\n\n1 - region\n2 - area\n3 - population\nYour choose: ");
fflush(stdin); }
printf("Choose order\n\n1 - ascending\n2 - descending\nYour choose: ");
while(scanf("%d%c", &variant_sort, &symbol) != 2 || symbol != '\n' || variant_sort < 1 || variant_sort > 2) {
printf("\nInvalid input. Please, try again\n\n1 - ascending\ndescending\nYour choose: ");
fflush(stdin); }
switch(choose_sort) {
case 1: {
for(int i = 0; i < x - 1; i++) {
for(int j = i + 1; j < x; j++) {
if(strcmp(ar[i].region, ar[j].region) > 0) {
record e = ar[i];
ar[i] = ar[j];
ar[j] = e;
}
}
}
break;
}
case 2: {
for(int i = 0; i < x - 1; i++) {
for(int j = i + 1; j < x; j++) {
if(ar[i].area > ar[j].area) {
record e = ar[i];
ar[i] = ar[j];
ar[j] = e;
}
}
}
break;
}
case 3: {
for(int i = 0; i < x - 1; i++) {
for(int j = i + 1; j < x; j++) {
if(ar[i].population > ar[j].population) {
record e = ar[i];
ar[i] = ar[j];
ar[j] = e;
}
}
}
break;
}
}
if (variant_sort == 1) {
for (int i = 0; i < x; i++) {
fprintf(f,"Region: %s Area: %lf Population:%d\n", ar[i].region, ar[i].area, ar[i].population);
}
} else {
for (int i = x - 1; i >= 0; i--) {
fprintf(f, "Region: %s Area: %lf Population: %d\n", ar[i].region, ar[i].area, ar[i].population);
}
}
fclose(ftemp);
fclose(f);
remove(file_name);
rename(temp, file_name);
break;
}
As well as the function itself
struct record parse_line(char* line) {
struct record record;
memset(record.region, 0, sizeof(record.region)); // Clear the region field
int spaces=0;
// Read the region field
int i = 0;
while (line[i] != ' ') {
record.region[i] = line[i];
i++;
spaces++;
}
i++; // Skip the space character
// Read the area field
char* area_str = malloc(50); // Allocate memory to hold the area string
memset(area_str, 0, 50); // Clear the memory
int space = 0;
int j = 0;
while (line[i] != ' ') {
area_str[j] = line[i];
i++;
space++;
}
j++;
record.area = atof(area_str); // Convert the area string to a double and store it in the record
free(area_str); // Free the memory allocated for the area string
// Read the population field
char* population_str = malloc(50); // Allocate memory to hold the population string
memset(population_str, 0, 50); // Clear the memory
j = 0;
while (line[i] != '\n' && line[i] != '\0') {
population_str[j] = line[i];
i++;
}
j++;
record.population = atoi(population_str); // Convert the population string to an int and store it in the record
free(population_str); // Free the memory allocated for the population string
return record;
}
Can anyone tell me why this is not working properly? I declare x as the upper bound for the outer loop and x - 1 as the upper bound for the inner loop. And it should all work, but nothing happens. I have already dug through everything and so I do not understand. Do you have any ideas?
If I choose sorting by population in ascending form, and then I go to the file, then nothing happens there