#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.
There are many problems in your code:
chmust be defined with typeintto store all possible values returned byfgetc()and properly distinguishEOFfrom all valid byte values,you should test the return value of
scanf()to detect input errors. Each of your calls toscanf()must return1for success because there is a single conversion attempted in each call.scanf("%[^\n]s", s.name);is invalid for 2 reasons: thesafter the]is useless, and you should pass the maximum number of characters to store tos.nameto avoid buffer overflows on invalid input. Use this instead:the second
scanf("%[^\n]s", s.branch);will fail because the newline was left pending in the input stream by the previous call toscanf(). You should flush the rest of the input line with this:you use a different conversion for the second entry:
%s, thus not allowing embedded spaces in the name or branch.you output both entries to
fp1: the secondfprintf()has undefined behavior becausefp1has been closed.fprintf(fp1, "\nName: %s\nBranch: %s\nAge: %d\nRoll.No: %d", ...)outputs the data on 5 separate lines starting with a blank line, and ending the file without a trailing newline. This is inadequate. You should instead output the data on a single line with a trailing newline:External sorting can be performed using the command line utility
sort, but the option to sort based on the last numeric field are non trivial. Writing code to perform this sorting in C seems challenging. If you can assume that both files are entered in the expected order, you could merge them as you read them, one line at a time, comparing the last field as a number and writing the smaller one until one of the files is empty: