#include "stdafx.h"
#include <iostream>
#include <fstream>
using namespace std;
const char fileName[50] = "c:\\Users\\use r\\Desktop\\lab 4prac\\data.txt ";
void openfile(ofstre am& outstream);
void closefile(ofstr eam& outstarem);
void writeInformatio n(ofstream& outstream, char firstName[], char lastName[], int phoneNumber);
void readInformation (char searchString[]);
void read();
bool checkInformatio n(char lastName[], char searchString[]);
bool checkInformatio n (char lastName[], char searchString[])
{
for (int i= 0; i<sizeof(search String); i++)
{
if (lastName[i] != searchString[i])
return false;
else
continue;
}
return true;
}
void openFile(ofstre am& outstream)
{
outstream.open( fileName, ios::app);
}
void closeFile(ofstr eam& outstream)
{
outstream.close ();
}
void writeInformatio n(ofstream& outstream, char firstName[], char lastName[], int phoneNumber)
{
outstream<<firs tName<<"\t"<<la stName<<"\t"<<p honeNumber<<end l;
}
void readInformation (char searchString[])
{
char firstName[10], lastName[10];
int phoneNumber;
bool recordFound = false;
ifstream instream;
instream.open(f ileName);
instream>>first Name>>lastName> >phoneNumber;
while(!instream .eof())
{
if(checkInforma tion(lastName, searchString))
{
cout<<"\nFirst Name and Last Name : "<<firstName<<" \t"<<lastName<< endl;
cout<<"\nPhone Number : "<<phoneNumber< <endl;
recordFound = true;
break;
}
instream>>first Name>>lastName> >phoneNumber;
}
instream.close( );
cout<<"Reading Completed ...."<<endl;
if(recordFound == false)
{
cout<<"Record not Found ... "<<endl;
}
}
void read()
{
char firstName[10], lastName[10];
int phoneNumber;
ifstream instream;
instream.open(f ileName);
if(instream.fai l())
{
cout<<"Invalid file or file is corrupted!!"<<e ndl;
exit(1);
}
else
{
int counter = 0;
instream>>first Name>>lastName> >phoneNumber;
while(!instream .eof())
{
cout<<"\nFirst Name and Last Name : "<<firstName<<" \t"<<lastName<< endl;
cout<<"\nPhone Number : "<<phoneNumber< <endl;
counter ++ ;
instream>>first Name>>lastName> >phoneNumber;
}
cout<<"Reading Completed : \n";
}
instream.close( );
}
int _tmain(int argc, _TCHAR* argv[])
{
int choice;
cout.setf(ios:: fixed);
cout.setf(ios:: showpoint);
cout.precision( 2);
ofstream outstream;
openFile(outstr eam);
cout<<"\n\t 1: Write Date to File \n";
cout<<"\t 2: Read Data from File \n";
cout<<"\t 3: Search Data from file using Last Name \n";
cout<<"\t 4: Quit \n";
cout <<"Your Choice: ";
cin>>choice;
switch(choice)
{
case 1:
int phoneNumber;
char firstName[10], lastName[10];
cout<<"Enter the First Name : ";
cin>>firstName;
cout<<"Enter the Last Name : ";
cin>>lastName;
cout<<"Enter the Phone Number : ";
cin>>phoneNumbe r;
writeInformatio n(outstream, firstName, lastName, phoneNumber);
cout<<"Writing Completed ... "<<endl;
break ;
case 2:
read();
break;
case 3:
cout<<"Enter the Last Name to search from the file : ";
cin>>lastName;
readInformation (lastName);
break;
case 4:
cout<<"\nGOOD BYE !!!! \n";
break;
closeFile(outst ream);
}
system ("PAUSE");
return 0;
}
#include <iostream>
#include <fstream>
using namespace std;
const char fileName[50] = "c:\\Users\\use r\\Desktop\\lab 4prac\\data.txt ";
void openfile(ofstre am& outstream);
void closefile(ofstr eam& outstarem);
void writeInformatio n(ofstream& outstream, char firstName[], char lastName[], int phoneNumber);
void readInformation (char searchString[]);
void read();
bool checkInformatio n(char lastName[], char searchString[]);
bool checkInformatio n (char lastName[], char searchString[])
{
for (int i= 0; i<sizeof(search String); i++)
{
if (lastName[i] != searchString[i])
return false;
else
continue;
}
return true;
}
void openFile(ofstre am& outstream)
{
outstream.open( fileName, ios::app);
}
void closeFile(ofstr eam& outstream)
{
outstream.close ();
}
void writeInformatio n(ofstream& outstream, char firstName[], char lastName[], int phoneNumber)
{
outstream<<firs tName<<"\t"<<la stName<<"\t"<<p honeNumber<<end l;
}
void readInformation (char searchString[])
{
char firstName[10], lastName[10];
int phoneNumber;
bool recordFound = false;
ifstream instream;
instream.open(f ileName);
instream>>first Name>>lastName> >phoneNumber;
while(!instream .eof())
{
if(checkInforma tion(lastName, searchString))
{
cout<<"\nFirst Name and Last Name : "<<firstName<<" \t"<<lastName<< endl;
cout<<"\nPhone Number : "<<phoneNumber< <endl;
recordFound = true;
break;
}
instream>>first Name>>lastName> >phoneNumber;
}
instream.close( );
cout<<"Reading Completed ...."<<endl;
if(recordFound == false)
{
cout<<"Record not Found ... "<<endl;
}
}
void read()
{
char firstName[10], lastName[10];
int phoneNumber;
ifstream instream;
instream.open(f ileName);
if(instream.fai l())
{
cout<<"Invalid file or file is corrupted!!"<<e ndl;
exit(1);
}
else
{
int counter = 0;
instream>>first Name>>lastName> >phoneNumber;
while(!instream .eof())
{
cout<<"\nFirst Name and Last Name : "<<firstName<<" \t"<<lastName<< endl;
cout<<"\nPhone Number : "<<phoneNumber< <endl;
counter ++ ;
instream>>first Name>>lastName> >phoneNumber;
}
cout<<"Reading Completed : \n";
}
instream.close( );
}
int _tmain(int argc, _TCHAR* argv[])
{
int choice;
cout.setf(ios:: fixed);
cout.setf(ios:: showpoint);
cout.precision( 2);
ofstream outstream;
openFile(outstr eam);
cout<<"\n\t 1: Write Date to File \n";
cout<<"\t 2: Read Data from File \n";
cout<<"\t 3: Search Data from file using Last Name \n";
cout<<"\t 4: Quit \n";
cout <<"Your Choice: ";
cin>>choice;
switch(choice)
{
case 1:
int phoneNumber;
char firstName[10], lastName[10];
cout<<"Enter the First Name : ";
cin>>firstName;
cout<<"Enter the Last Name : ";
cin>>lastName;
cout<<"Enter the Phone Number : ";
cin>>phoneNumbe r;
writeInformatio n(outstream, firstName, lastName, phoneNumber);
cout<<"Writing Completed ... "<<endl;
break ;
case 2:
read();
break;
case 3:
cout<<"Enter the Last Name to search from the file : ";
cin>>lastName;
readInformation (lastName);
break;
case 4:
cout<<"\nGOOD BYE !!!! \n";
break;
closeFile(outst ream);
}
system ("PAUSE");
return 0;
}
Comment