c++ - Sorting files problem
Dec 14, 2011 at 4:26am UTC
I am trying to sort a record according to their respective number like this:
1 2 3 4 5
5 reyes d r 1 2 3
3 delos d k 4 5 6
9 go t r 7 4 5
1 po w w 2 2 2
2 bun b m 3 3 3
then it will become:
1 2 3 4 5
1 po w w 2 2 2
2 bun b m 3 3 3
3 delos d k 4 5 6
5 reyes d r 1 2 3
9 go t r 7 4 5
here is the definition of the 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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
void sortRec()
{
fstream data,temp;
string line;
int size(0);
int id[500];
data.open("employee.txt" ,ios_base::in);
//counting lines
data.seekg(0,ios::beg);
while (getline(data,line))
{size++;}
data.clear();
data.seekg(0,ios::beg);
for (int i=0;i<size;i++)
{
data>>id[i]>>e.name.lname>>e.name.fname>>e.name.mname>>e.address>>e.hour>>e.hRate;
}
//sorting employee numbers
for (int a=0;a<size;a++)
{
for (int b=a+1;b<size;b++)
{
if (id[b]<id[a])
{
int tmp;
tmp=id[a];
id[a]=id[b];
id[b]=tmp;
}
}
}
data.close();
//sorting the data
rename("employee.txt" ,"tempfile.txt" );
data.open("employee.txt" ,ios_base::app);
temp.open("tempfile.txt" ,ios_base::in);
int _id;
for (int i=0;i<size;i++)
{
temp>>_id;
temp>>e.name.lname>>e.name.fname>>e.name.mname>>e.address>>e.hour>>e.hRate;
for (int a=0;a<size;a++)
{
if (id[a]==_id)
{data<<_id<<" " <<e.name.lname<<" " <<e.name.fname<<" " <<e.name.mname<<" " <<e.address<<" " <<e.hour<<" " <<e.hRate<<'\n' ;}
}
}
data.close();
temp.close();
remove("tempfile.txt" );
}
what i did is i get the E numbers of every record first then i sort them. then i open the file again and get the E numbers to compare to the sorted one then write it to the file.
but what happen is, it didn't sort, the files on the record remains the same :
1 2 3 4 5 6
5 reyes d r 1 2 3
3 delos d k 4 5 6
9 go t r 7 4 5
1 po w w 2 2 2
2 bun b m 3 3 3
Do anyone knows where in my code go wrong?
Dec 14, 2011 at 5:12am UTC
1)
then i open the file again
You are opened the file in append mode, so original content present in the file (it was not written by your code)
data.open("employee.txt" ,ios_base::app );
2) Your way of sorting is correct
3) Writing the sorted information to the file was problem
Replace the following code
1 2 3 4 5 6 7 8 9 10 11 12
int _id;
for (int i=0;i<size;i++)
{
temp>>_id;
temp>>e.name.lname>>e.name.fname>>e.name.mname>>e.address>>e.hour>>e.hRate;
for (int a=0;a<size;a++)
{
if (id[a]==_id)
{data<<_id<<" " <<e.name.lname<<" " <<e.name.fname<<" " <<e.name.mname<<" " <<e.address<<" " <<e.hour<<" " <<e.hRate<<'\n' ;}
}
}
with
1 2 3 4 5 6 7 8 9 10 11 12 13 14
int _id;
for (int i=0;i<size;i++)//for iterating id
{
for (int a=0;a<size;a++)
{
temp>>_id;
temp>>e.name.lname>>e.name.fname>>e.name.mname>>e.address>>e.hour>>e.hRate;
if (id[i]==_id)
{
{data<<_id<<" " <<e.name.lname<<" " <<e.name.fname<<" " <<e.name.mname<<" " <<e.address<<" " <<e.hour<<" " <<e.hRate<<'\n' ;}
break ;
}
}
}
Dec 14, 2011 at 5:33am UTC
thanks ^^
Topic archived. No new replies allowed.