Sort() function problem

Problem :
--->I cannot sort the records in the file CONSUMER.DAT
--->Help me with this part of code

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
void sort()
{
	BILL a;
	int x[100];
	int *p=x;
	int count=0;
	ifstream fin("CONSUMER.DAT",ios::binary);
	ofstream fout("REPORT.DAT",ios::binary);
	fin.seekg(0);
	while(fin.read((char*)&a,sizeof(a)))
	{
		count++;
		*p=a.get_sub_no();
		p++;             //           <-------------there is a problem here
	}
	int temp,pos,small;
	for(int i=0;i<(count-1);i++)
	{
		small=x[i];
		pos=i;
		for(int j=i+1;j<count;j++)
		{
			if(x[j]<small)
			{
				small=x[j];
				pos=j;
			}
		}
		temp=x[i];
		x[i]=x[pos];
		x[pos]=temp;
	}
	fin.seekg(0);
	for(i=0;i<count;i++)
	{
		while(fin.read((char*)&a,sizeof(a)))
		{
			if(x[i]==a.get_sub_no())
				fout.write((char*)&a,sizeof(a));
				break;
		}
	}
	fin.close();
	fout.close();
	cout<<"\nSorting Was Successful !!!";
}


please help me in this part !!!




Note:
--->Compiler : Turbo C++ for Windows 7
--->Version : 3.7.8.9m_r
--->By : Neu Tron
Last edited on
> there is a problem here
¿what problem?
¿how did you detect it?


lines 16--32: unless your sort is the problem, I don't want to analyse it
std::sort(x, x+count);


lines 34--42: files are not circular, once you reach the end of the file you can't keep reading.


> a.get_sub_no();
I cannot compile that, cannot run it, cannot debug it.
At most you can get code review by tired eyes.
Did This Way :

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
#include <fstream.h>
#include <conio.h>

void sort();
void view_file();

class BILL
{
	private:
		int sub_no;
		char sub_name[80];
		int unit;
		char category;
		float slab[5];
		float amount;
	public:
		int get_sub_no(){return sub_no;}
		void cdisp();
};

void BILL::cdisp()
{
	cout<<sub_no<<"\t"<<sub_name<<"\t\t\t"<<category<<"\t\t"<<unit<<"\t"<<amount;
}

void main()
{
	clrscr();
	sort();
	view_file();
	getch();
}

void sort()
{
	clrscr();
	BILL a;
	int x[10],i=0,count=0,small,pos,temp;
	ifstream fin("CONSUMER.DAT",ios::binary);
	ofstream fout("REPORT.DAT",ios::binary);
	fin.seekg(0);
	while(fin.read((char*)&a,sizeof(a)))
		count++;
	fin.seekg(0);
	while(fin.read((char*)&a,sizeof(a)))
	{
		for(;i<count;)
		{
			x[i]=a.get_sub_no();
			i++;
			break;
		}
	}
	fin.seekg(0);
	for(i=0;i<(count-1);i++)
	{
		small=x[i];
		pos=i;
		for(int j=0;j<count;j++)
		{
			if(x[j]<small)
			{
				small=x[j];
				pos=j;
			}
		}
		temp=x[i];
		x[i]=x[pos];
		x[pos]=temp;
	}
	for(i=0;i<count;i++)
	{
		while(fin.read((char*)&a,sizeof(a)))
		{
			if(x[i]==a.get_sub_no())
			{
				fout.write((char*)&a,sizeof(a));
				break;
			}
			else
			{
				goto A;	
			}
		}
		A:
	}
	fin.close();
	fout.close();
}

void view_file()
{
	clrscr();
	BILL a;
	ifstream fin("REPORT.DAT",ios::binary);
	fin.seekg(0);
	cout<<"\nNo\tName\t\t\tCategory\tUnit\tAmount";
	cout<<"\n--\t----\t\t\t--------\t----\t------\n";
	while(fin.read((char*)&a,sizeof(a)))
	{
		a.cdisp();
		cout<<"\n";
	}
	fin.close();
}

//Problem in Code : "REPORT.DAT" File is Found Blank
//Length:1667_Lines:108 
in line 45: while(fin.read((char*)&a,sizeof(a)))
when that loop ends the file will be in an invalid state, so the fin.seekg(0); (line 54) will fail.

You may fin.clear() before the seek to make it valid again, so the reading in line 73 would work.
However, your algorithm is incorrect, suppose that the smallest element is in the last register of the archive, then that would be the only one writed to the output file.
Topic archived. No new replies allowed.