#include <stdio.h>
#include <stdlib.h>
struct student {
    int roll, age;
    char name[30], branch[30];
} s;
int main() {
    FILE *fp1, *fp2, *fp3;
    int num1, num2, i;
    char ch;
    printf("Enter the Number of Students to compute the data in 1st file: ");
    scanf("%d", &num1);
    fp1 = fopen("Dbms lab-1 1sttxt.txt", "w");
    for (i = 0; i < num1; i++) {
        printf("\nEnter the Name of the Student: ");
        scanf("%[^\n]s", s.name);
        printf("Enter the Branch the Student is studying in: ");
        scanf("%[^\n]s", s.branch);
        printf("Enter the Age of the Student: ");
        scanf("%d", &s.age);
        printf("Enter the Roll Number: ");
        scanf("%d", &s.roll);
        fprintf(fp1, "\nName: %s\nBranch: %s\nAge: %d\nRoll.No: %d",
                s.name, s.branch, s.age, s.roll);
    }
    printf("The Record is stored in the file.");
    fclose(fp1);
    printf("\nEnter the Number of Students to compute data in 2nd file: ");
    scanf("%d", &num2);
    printf("\n\n Enter Student details in the second file");
    fp2 = fopen("Dbms lab-1 2ndtxt.txt", "w");
    for (i = 0; i < num2; i++) {
        printf("\n\nEnter the Name of the Student: ");
        scanf("%s", s.name);
        printf("\nEnter the Branch the Student is studying in: ");
        scanf("%s", s.branch);
        printf("\nEnter the Age of the Student: ");
        scanf("%d", &s.age);
        printf("\nEnter the Roll Number: ");
        scanf("%d", &s.roll);
        fprintf(fp1, "\nName: %s\nBranch: %s\nAge: %d\nRoll.No: %d",
                s.name, s.branch, s.age, s.roll);
    }
    printf("\nThe Record is stored in the file.");
    fclose(fp2);
    fp1 = fopen("Dbms lab-1 1sttxt.txt", "r");
    fp2 = fopen("Dbms lab-1 2ndtxt.txt", "r");
    fp3 = fopen("Dbms lab-1 3rdtxt.txt", "w");
    if (fp1 == NULL || fp2 == NULL || fp3 == NULL) {
        printf("Could not open the file,because the file is empty");
        exit(0);
    }
    while ((ch = fgetc(fp1) != EOF)) {
        putc(ch, fp3);
    }
    while ((ch = fgetc(fp2) != EOF)) {
        putc(ch, fp3);
    }
    printf("\nThe two files are successfully merged.");
    fclose(fp1);
    fclose(fp2);
    fclose(fp3);
    return 0;
}
I've merged data from 2 files into a single file. Now I want to perform external sorting in the third file. I want to sort the data based on the value of roll. How do I do it ? Can you help me with the code? Thank you.
 
     
    