txt file

by using text file how to make a searching code



#include<iostream>
#include<fstream>
using namespace std;


struct inf
{

string fname;
string mname;
string lname;
int age;
string status;


void display()

{

cout<<"\n\n\nFirst Name: "<<fname;
cout<<"\nMidle Name: "<<mname;
cout<<"\nLast Name: "<<lname;
cout<<"\nAge: "<<age;
cout<<"\nStatus :"<< status;
}
};

main()
{
ofstream out;
ifstream in;



out.open("C:\\MyPro\\Output Sample File.txt",ios::app);
in.open("C:\\MyPro\\MyInFile.txt",ios::in);

if(out.is_open())
cout<<"\n\nOutput File is now open for any transaction:";

if(in.is_open())
cout<<"\n\nInput File is now open for any transaction:";
else
cout<<"\n\nFailed to open your input file";

inf *person;

person = new inf[100];

for(int i=0;i<100;i++)
in>>person[i].fname>>person[i].mname>>person[i].lname>>person[i].age>>person [i].status;

for(int j=0;j<100;j++)
{
cout<<j;
cout<<"\n";

person[j].display();
}





in.close();
out.close();
cout<<"\n\n";

system("PAUSE");
return 0;
}
Please, clarify your question.

Use of code tags on the post is strongly recommended.

Do not use system("PAUSE");. There are portable alternatives. See the sticky thread.

Consider implementing an operator>> for inf.

Are you sure that the file has at least 100 entries?

You don't deallocate at end the memory allocated by new[]. You should. Better yet, you could consider standard containers instead of new[], or at least a smart pointer type instead of plain pointer.
Topic archived. No new replies allowed.