In this program i am writing to a file, reading the values to a array and then sort it based on id which is a char. Got it to read and write but need help with sorting it.
Here is the code:
Code:
#include <stdio.h>
#include <iostream.h>
#include <conio.h>
#include <fstream.h>
#include <string.h>
#include <stdlib.h>


class person
{
  public: char id[5];
/*	  char name[15];
	  char age[5];
	  char address[15];*/
	  char buffer[50];
};
fstream val,file;
person p;
char id[5];
void write()
{
 val.open("person4.txt",ios::in);
 if(!val)
 {
  cout << "Cannot open file" << endl;
  exit(1);
 }

 cout << "Enter ID: " ;
 cin >> id;

while(!val.eof())
 {
  val.getline(p.id,5,'|');
   if(strcmp(id,p.id)==0)
     {
      cout << "ID already exists" << endl;
      val.close();
      getch();
      return;

     }
 }
 val.close();
 strcpy(p.id,id);
 file.open("person4.txt",ios::app);
 if(!file)
 {
 cout << "Cannot open file" << endl;
 exit(1);
 }
/* cout << "Enter name: " ;
 cin >> p.name;
 cout << "Enter age: ";
 cin >> p.age;
 cout << "Enter address: ";
 cin >> p.address;*/
 strcpy(p.buffer,p.id);
 strcat(p.buffer,"|");
/* strcat(p.buffer,p.name);
 strcat(p.buffer,"|");
 strcat(p.buffer,p.age);
 strcat(p.buffer,"|");
 strcat(p.buffer,p.address);
 strcat(p.buffer,"!"); */
 strcat(p.buffer,"\n");
 file << p.buffer;
 file.close();
}
void display()
{
 int i,n;
 person p[20];
 char* temp;//temp[20];


 file.open("person4.txt",ios::in);
 if(!file)
 {
  cout << "Cannot open file!" << endl;
  getch();
  exit(1);
 }
 n=0;
 i=0;
 cout << "ID\t\tName\t\tAge\t\tAddress" << endl;
 cout << "....\t\t..\t\t...\t\t..." << endl;
 while(!file.eof())
 {


   file.getline(p[i].id,5,'|');
   cout<<"p[i] id and i value"<<i<<p[i].id<<endl;
   //file.getline(p[i].name,15,'|');
  //file.getline(p[i].age,5,'|');
   //file.getline(p[i].address,15,'!');
   i++;
   n++;

 }

 file.close();

  for(int j=0;j<n;j++)
    {
       for(int k=j+1;k<n;k++)
       {
	    if(strcmp(p[j].id,p[k].id)>= 0)
	    {
	       strcpy(temp,p[k].id);//(temp[j],p[k].id);
	      
	       strcpy(p[k].id,p[j].id);
	       strcpy(p[j].id,temp);//(p[j].id,temp[k]);
	    }
       }
    }


for(i=0;i<n;i++)
	cout<<p[i].id<<endl;


// file.close();
 getch();
}

void main()
{

 int choice;

 while(1)
 {
  clrscr();
  cout << "0: Exit: " << endl;
  cout << "1: Write: " << endl;
  cout << "2: Display: " << endl;
  cin >> choice;

  switch(choice)
  {
   case 0: exit(0);
   break;
   case 1: write();
   break;
   case 2: display();
   break;
   default: cout << "invalid";
   break;

  }

 }

}