How do I sort in 3 files alphabetically? How to sort the alphabet using the student's last name before writing to the file. Ie the file should be recorded already sorted list. Sample code .

Initial data:

  • student[i].Family - Last name student
  • student[i].Name - Student Name
  • student[i].Sex - Paul
  • student[i].Date[0] - number
  • student[i].Date[1] - month
  • student[i].Date[2] - year of birth
  • student[i].City - city of birth

      case 2: char f4[20]; FILE *z1; cout << "\nВведите имя файла для чтения: "; //fscanf(v, "%d", &m); cin >> f4; z1 = fopen(f4, "r"); for (int i = 0; i < N; i++) fscanf(z1, "%s%s%s%d%d%d%s", &student[i].Family, &student[i].Name, &student[i].Sex, &student[i].Date[0], &student[i].Date[1], &student[i].Date[2], &student[i].City); cout << "\nЧтение из файла " << f4 << ":\n"; for (int i = 0; i < N; i++) cout << student[i].Family << " " << student[i].Name << " " << student[i].Sex << " " << student[i].Date[0] << "." << student[i].Date[1] << "." << student[i].Date[2] << " " << student[i].City << "\n"; cout << "File name? "; char girls[50]; cin.getline(girls, 50); f = fopen("girls.txt", "a+"); for (int i = 0; i < N; i++) if (strcmp(student[i].Sex, "Female") == 0) { student[i].Show(); } //сортировка по алфавиту это то что я сам сейчас попробовал но это не работает походу while (a == 1) { a = 0; for (int i = 0; i<N - 1; i++){ if (strcmp(student[i].Family, student[i + 1].Family)>0) { strcpy(tmp, student[i].Family); strcpy(student[i].Family, student[i + 1].Family); strcpy(student[i + 1].Family, tmp); /*strcpy(tmp, fr[i].dolgnost); strcpy(fr[i].dolgnost, fr[i + 1].dolgnost); strcpy(fr[i + 1].dolgnost, tmp); tmp1 = fr[i].stag; fr[i].stag = fr[i + 1].stag; fr[i + 1].stag = tmp1;*/ a = 1; } } }// вот до этого момента новое. fclose(f); //С помощью цикла показываем всех девочек. system("pause"); break; 
  • Sort let's say by last name, but I do not understand how to do it. - papalimer
  • one
    Please clarify the question. What exactly you don't understand: how to open a stream for reading, how to read a student's structure from a stream, how to put read structures into a vector, how to sort a vector using your comparison function, how to search for a comparison function for a student that will compare by last name, how to write data back to the file? - dzhioev Nov.
  • @zhzhioev How to sort the alphabetical order using the student's name, before writing to the file. Ie the file should be recorded in the already sorted list - papalimer
  • @BalinMisha if you have a problem with sorting, then it is better to rename the question, for example, in "How to make a list sorting" and remove the excess from the question, which does not concern sorting - Bald
  • @ Bald56rus Yeah, thanks. I will wait, mb someone will tell - papalimer

1 answer 1

in the comments already mentioned that you can use the collection and the sort function (). Create a collection. Fill the list with values.

  list<Student> lines; 

Redefine the operator more (or less) for your structure.

 bool Student::operator < (const Student& rhs) const { return ... } 

Then use the sort function ().

 lines.sort(); 

You can also compare the codes of letters from ASCII .

Here is an example of a simple program using character codes:

 char Name1 [5]={'B','C','D','Z','Y'}; char Name2 [5]={'A','c','E','T','k'}; for(int i =0 ; i<5;i++){ cout<<Name1[i]-Name2[i]<<" "; } 

At the exit:

 1 -32 -1 6 -18 

In your case, it is necessary to apply the usual sorting method, so that at the beginning of the list there is a last name with the smallest code of the first character.

  • Thank you, you understood correctly. But it is not that. - papalimer
  • @BalinMisha, And what exactly is not suitable method? Maybe someone else's answer to this question will push on the correct answer. - Sanek Zhitnik