merging two files

I am trying to output data from two sorted arrays into one file(which should also be sorted). The program works for most of the data, but after it reaches a certain record begins outputting garbage. I'm sure part of the problem is that I don't know what to include in my while loop that outputs to the file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

struct student
{
	string firstName, lastName;
	float gpa;
	int hours;
};

ostream& operator<<(ostream& os, student A)
{
	os<<A.firstName<<"\t"<<A.lastName<<"\t"<<A.gpa<<"\t"<<A.hours;
	return os;
}

istream& operator>>(istream& is, student& A)
{
	is>>A.firstName>>A.lastName>>A.gpa>>A.hours;
	return is;
}


void swap(student[], int i, int j);
void sort(student[], int size);

int main()
{
	student A[100];
	student B[100];

	ifstream fin1, fin2;
	ofstream fout;

	fin1.open("merge1.txt");

	int k=0;

	while(fin1>>A[k])
	{
		k++;
	}

	fin1.close();

	sort(A, k);
	

	fin2.open("merge2.txt");

	int j=0;

	while(fin2>>B[j])
	{
		j++;
	}
	
	fin2.close();

	sort(B, j);	

	
	fin1.open("merge1.txt");
	fin2.open("merge2.txt");
	fout.open("merged.txt");

	int m=0;
	int p=0;

	while(m!=100 && p!=100)
	{
		if(A[m].lastName<B[p].lastName)
		{
			fout<<A[m]<<endl;
			m++;
		}
		else
		{
			fout<<B[p]<<endl; 
			p++;
		}
	}






return 0;
}

void swap(student A[], int i, int j)
{
	student temp;
	temp=A[i];
	A[i]=A[j];
	A[j]=temp;

	return;
}

void sort(student A[], int size)
{
	for(int p=1; p<size; p++)
	{
		for(int c=0; c<size-p; c++)
		{
			if(A[c].lastName>A[c+1].lastName) swap(A, c, c+1);
		}
	}
	
	return;
}
line 72 while(m!=100 && p!=100)
seems suspicious.

Where did the 100s come from? The sizes of A and B are 100 doesn't mean that they contain 100 valid records. As both the files "merge1.txt" and "merge2.txt" probably contain less than 100 records each, A and B contain less than 100 valid records. The rest of the array contains garbage.

Try this instead
while(m<k && p<j)

because k and j are the actual no. of records in A and B.
Thank you, that fixed the problem of garbage. Now, the only problem is that all of the sorted records except two are outputted to the file.
In merging, you have take care of this:

Consider that there are 30 records in the first file and 3 in the second. As soon as the 3 records of the 2nd file are written to the output file, the while loop will stop and the remaining records of the first file will not be written to the output file.

Add this after the last while loop
1
2
3
4
while(m<k)fout<<A[m++]<<endl;
//output all remaining records of A to file
while(p<j)fout<<B[p++]<<endl;
//output all remaining records of B to file 


Note that only one of these while loops will run because after leaving the loop at line 72, while(m<k && p<j), either m is equal to k or p is equal to j.
Thank you, I now understand the concept and this solved my issue.
Topic archived. No new replies allowed.