Using an editor ,create two text files Each record in two text files will contai
ID: 3618761 • Letter: U
Question
Using an editor ,create two text files Each record in two text files will contain an integer student ID followed by the student name,score1,score2. Here are two sample: 11 john 87 65 37 Merry 93 72 You can randomly create these samples to the two files. Read the two files into your program, and print out the smallest and largest ID of the all the files. Write a subroutine "merge" that will merge the contents of two text files containing students scores sorted by student ID and will produce a sorted file of binary record (create a binary file) The parameters of the subroutine "merge" will be three file pointers. The function can assume that one file does not have two copies of the same data and the binary output file should have this same property. Hint: When one of the input files is exhausted ,do not forget to copy the remaining data of the others input file to the result file.Explanation / Answer
#include<iostream.h>
#include<stdio.h>
#include<stdlib.h>
struct student
{
int id;
char name[50];
int score1;
int score2;
};
int totalRecords = 0;
struct student students[100];
void sort()
{
for(int i=0;i<totalRecords;i++)
{
for(int j=0;j<totalRecords;j++)
{
if(students[i].id < students[j].id)
{
struct student temp;
temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}
}
voiddisplaySortedRecords()
{
for(int i=0;i<totalRecords;i++)
{
cout<<" "<<students[i].id<<""<<students[i].name<<""<<students[i].score1<<""<<students[i].score2;
}
}
void merge(FILE *fp,FILE *fs,FILE *fb)
{
fp = fopen("Data1.txt","r");
fs = fopen("Data2.txt","r");
int numRecordsFile1= 0;
int numRecordsFile2 = 0;
fscanf(fp,"%d ", &numRecordsFile1);
fscanf(fs,"%d ", &numRecordsFile2);
totalRecords = numRecordsFile1 + numRecordsFile2;
int tmpID = 0;
int smallestNum = 0;
int largestNum = 0;
for(int i=0;i<numRecordsFile1;i++)
{
fscanf(fp,"%d ",&tmpID);
if(i == 0)
{
smallestNum = tmpID;
}
else if(smallestNum > tmpID)
{
smallestNum = tmpID;
}
if(largestNum < tmpID)
{
largestNum = tmpID;
}
students[i].id =tmpID;
fscanf(fp,"%s ",students[i].name );
fscanf(fp,"%d ", &students[i].score1 );
fscanf(fp,"%d ", &students[i].score2 );
}
for(int j =i;j<totalRecords;j++)
{
fscanf(fs,"%d ",&tmpID);
if(i == 0)
{
smallestNum = tmpID;
}
else if(smallestNum > tmpID)
{
smallestNum = tmpID;
}
if(largestNum < tmpID)
{
largestNum = tmpID;
}
students[j].id = tmpID;
fscanf(fs,"%s ",students[j].name );
fscanf(fs,"%d ", &students[j].score1 );
fscanf(fs,"%d ", &students[j].score2 );
}
cout<<" the smallest ID : "<<smallestNum;
cout<<" the largest ID :"<<largestNum;
cout<<" ";
fclose(fs);
fclose(fp);
sort();
displaySortedRecords();
fb =fopen("output.bin","wb");
if(fb!=NULL)
{
for(int i=0;i<totalRecords;i++)
{
fputc(students[i].id,fb);
fputs(students[i].name,fb);
fputc(students[i].score1,fb);
fputc(students[i].score2,fb);
fputc(' ',fb);
}
fclose(fb);
}
}
int main()
{
FILE *fp,*fs,*fb;
merge(fp,fs,fb);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.