sorting of binary file

I was trying to sort the binary file wthin the same without using array w.r.t. roll number of the student but the program is not working.
file already has contents in it.
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
class student
{
 char name[20];
 int rollno;
 int marks;
 public:
 void getdata()
  {
  cout<<"Enter the name of the student:";
  gets(name);
  cout<<"Enter the rollno of the student:";
  cin>>rollno;
  cout<<"Enter the marks of the student:";
  cin>>marks;
 }
 void putdata()
  {
  cout<<"\nName:"<<name<<endl;
  cout<<"Rollno:"<<rollno;
  cout<<"\tMarks:"<<marks;
 }
 int getrno()
 {
  return rollno;
 }
};

void insert()
{
 student insert;
 ofstream f("file2.dat",ios::binary|ios::app);
 if(!f)
 {
  cout<<"file didn't open";
  getch();
  return;
 }
 insert.getdata();
 f.write((char*)&insert,sizeof(insert));
 f.close();
}

void Delete()
{
 ifstream f1("file2.dat",ios::binary);
 ofstream f2("temp.dat",ios::binary);
 student del;
 int rollno;
 f1.seekg(0);
 cout<<"Enter the roll no of the record whose record to be deleted:";
 cin>>rollno;
 char record='n';
 while(!f1.eof())
  {
   f1.read((char*)&del,sizeof(del));
   if(del.getrno()!=rollno)
   f2.write((char*)&del,sizeof(del));
   else
   record='y';
  }
  if(record=='n')
  cout<<"Record does not exist";
  else
  cout<<"Record with roll number "<<rollno<<" has been deleted";
 f1.close();
 f2.close();
 remove("file2.dat");
 rename("temp.dat","file2.dat");
}

void display()
{
 student disp;
 ifstream f("file2.dat",ios::binary);
 while(f.read((char*)&disp,sizeof(disp)))
 {
  disp.putdata();
 }
}
void Sort()
{
 fstream f1("file2.dat",ios::binary|ios::ate|ios::in|ios::out);
 int i=0;
 student obj1,obj2;
 long size=f1.tellg()/sizeof(student);
 for(i=0;i<size-1;++i)
 {
  for(int j=i+1;j<size-1-i;++j)
  {
    f1.read((char*)&obj1,sizeof(obj1));
    f1.read((char*)&obj2,sizeof(obj2));
    if(obj1.getrno() > obj2.getrno())
    {
      f1.seekp(-48,ios::cur);
      f1.write((char*)&obj2,sizeof(obj2));
      f1.write((char*)&obj1,sizeof(obj1));
      f1.seekp(-24,ios::cur);
    }
  }
 }
 f1.close();
}

void main()
{
 clrscr();
 int choice1=0;
 char choice2='y';
 do
 {
  cout<<"\n1.Insert a record";
  cout<<"\t2.Display a record";
  cout<<"\n3.Delete a record";
  cout<<"\t4.Sort the file";
  cout<<"\t\t5.Exit\n";
  cout<<"Enter the choice:";
  cin>>choice1;
  switch(choice1)
  {
   case 1: insert();  break;
   case 2: display(); break;
   case 3: Delete();  break;
   case 4: Sort();    break;
   case 5: exit(0);   break;
   default: cout<<"\nWrong choice entered";
  }
  cout<<"\nFurther file handling(y/n):";
  cin>>choice2;
 }while(choice2=='y');
 getch();
}

Last edited on
What is the problem?
File is not getting sorted. It remain the same.
print out the number you compare from both objects each loop iteration, and a marker if you swap them.

In your Sort function in your inner loop on j between lines 92 and 102 you are reading twice from the file hence advancing the file pointer internally by two records however you are doing this on the first iteration when i=0 by the [number_of_records_in_the_file - 1]. This implies you are reading (/attempting to read) twice that number of records from the file which should render the file stream ended.

You should therefore rather be iterating your j loop at half of the cycles you do it currently to compensate for the double reads. Also after exiting your j loop you should reset the file pointer using the seek method to the next appropriate i'th based record.
Topic archived. No new replies allowed.