Wednesday, May 16, 2012

Prog-4


#include<stdio.h>

#include<stdlib.h>

#include<iostream.h>

#include<fstream.h>

#include<stdlib.h>

#include<conio.h>

#include<string.h>

#include<iomanip.h>

class student

{

  public:char name[15],usn[15],age[5],sem[5],branch[15],buffer[100];

};

void search()

{

  char usn[15];

  int i=0;

  student s1[100];

  cout<<"\nEnter the usn to be searched ";

  cin>>usn;

  fstream in;

 in.open("student.txt",ios::in);

 if(!in)

 {

   cout<<"\ncannot open the file in output mode";

   getch();

   exit(0);

 }



 i=0;

 printf("Name\tUsn\tAge\tSem\tBranch\n");

 while(!in.eof())

 {

  in.getline(s1[i].name,15,'|');

  in.getline(s1[i].usn,15,'|');

  in.getline(s1[i].age,5,'|');

  in.getline(s1[i].sem,5,'|');

  in.getline(s1[i].branch,15,'\n');

  i++;

 }



 for(int j=0;j<i-1;j++)

 {

   if(strcmp(usn,s1[j].usn)==0)

   {

    printf("\nRecord found");

    printf("\n%s\t%s\t%s\t%s\t%s",s1[j].name,s1[j].usn,s1[j].age,s1[j].sem,s1[j].branch);

    return;

   }

 }



 cout<<"\nRecord not found";

 return;



}



void writeRecord()

{



 fstream app;

 student s;

 app.open("student.txt",ios::app);

 if(!app)

 {

   cout<<"cannot open the file in output mode";

   getch();

   exit(0);

 }



 cout<<"\nEnter the student name       = ";

 cin>>s.name;

 cout<<"\nEnter the usn                = ";

 cin>>s.usn;

 cout<<"\nEnter the age                = ";

 cin>>s.age;

 cout<<"\nEnter the sem                = ";

 cin>>s.sem;

 cout<<"\nEnter the branch             = ";

 cin>>s.branch;

 strcpy(s.buffer,s.name);

 strcat(s.buffer,"|");

 strcat(s.buffer,s.usn);

 strcat(s.buffer,"|");

 strcat(s.buffer,s.age);

 strcat(s.buffer,"|");

 strcat(s.buffer,s.sem);

 strcat(s.buffer,"|");

 strcat(s.buffer,s.branch);

 strcat(s.buffer,"\n");



 app<<s.buffer;

 app.close();



}





void main()

{

 clrscr();



 int choice;



 fstream out;



  out.open("student.txt",ios::out);



 if(!out)

 {

   cout<<"cannot open the file in output mode";

   getch();

   exit(0);

 }



 out.close();



 for(;;)

 {

 cout<<"\n0:exit";

 cout<<"\n1:Insert";

 cout<<"\n2:Search";



 cout<<"\nEnter the choice = ";

 cin>>choice;



 switch(choice)

 {

  case 1:writeRecord();break;

  case 2:search();break;

  case 0:exit(0);

  default:cout<<"\nInvalid option";

 }

 }

}

No comments:

Post a Comment