error in file sorting

#include<fstream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
void main()
{ clrscr();
ofstream fout("a");
fout<<5<<"\n"<<3<<"\n"<<2<<"\n"<<4<<"\n"<<1;

fout.close();
ifstream fin("a");

int i,j,k,l=0,m,n,o;
while(fin)
{
fin>>i;
l++;
}
l--;
fin.close();
fstream fin1("a",ios::ate,ios::in);
fin1.seekg(0);
for(i=0;i<l-1;i++)
{ for(j=0;j<l-i-1;j++)
{ o=fin.tellg();
fin1>>m;
fin1>>n;
if(m>n)
{ fin1.seekg(o);
fin1<<n;
fin1<<m;
fin1.seekg(o+1);
}
}
}


}


file sorting is not working if anyone can help..



Last edited on
What do you want to do? Do you want to sort the elements in the file as follows?

Input file "a"
5
3
2
4
1

Output file "a"
1
2
3
4
5


Also in your present code,
1
2
3
4
5
6
7
while( fin ) // fin is not a boolean expression
{

  fin>>i; // Only the last element read from the file shall be stored in i. 
             // All the previous elements read shall be overwritten.
  l++;
}
Topic archived. No new replies allowed.