I have a read program and a write program. The first is just the write and I'm pretty sure it works. It compiles. My real problem comes with the next program. How am I supposed to make the program either display all of the file, or just all occurances of a certain name, or all of a certain name with only a certain level (i.e. am or noon or pm) Any help would be appreciated.
Here is the write program:
Code:
#include <iostream>
#include <iomanip>
#include <math.h>
#include <ctype.h>
#include <string.h>
#include <fstream>
using namespace std;
/******************************************************
Structure: person
******************************************************/
struct person
{
char personName[30];
int amLevel;
int noonLevel;
int pmLevel;
};
int main()
{
ofstream outFile;
person patient;
outFile.open("a:\\Z089933sugar.dat",ios::binary);
if(outFile.fail())
{
cout<< "Error! Make sure file exists.";
exit(1);
}
//1
strcpy(patient.personName,"Benjamin_Franklin");
patient.amLevel = 180;
patient.noonLevel = 200;
patient.pmLevel = 220;
outFile.write((char*)&patient,sizeof(person));
//2
strcpy(patient.personName,"Benjamin_Franklin");
patient.amLevel = 170;
patient.noonLevel = 190;
patient.pmLevel = 200;
outFile.write((char*)&patient,sizeof(person));
//3
strcpy(patient.personName,"Benjamin_Franklin");
patient.amLevel = 190;
patient.noonLevel = 200;
patient.pmLevel = 150;
outFile.write((char*)&patient,sizeof(person));
//4
strcpy(patient.personName,"Alex_Doe");
patient.amLevel = 140;
patient.noonLevel = 170;
patient.pmLevel = 150;
outFile.write((char*)&patient,sizeof(person));
//5
strcpy(patient.personName,"Mark_Doe");
patient.amLevel = 150;
patient.noonLevel = 175;
patient.pmLevel = 135;
outFile.write((char*)&patient,sizeof(person));
//6
strcpy(patient.personName,"John_Doe");
patient.amLevel = 100;
patient.noonLevel = 120;
patient.pmLevel = 110;
outFile.write((char*)&patient,sizeof(person));
//7
strcpy(patient.personName,"Alex_DoeII");
patient.amLevel = 130;
patient.noonLevel = 125;
patient.pmLevel = 145;
outFile.write((char*)&patient,sizeof(person));
//8
strcpy(patient.personName,"Chris_Doe");
patient.amLevel = 120;
patient.noonLevel = 150;
patient.pmLevel = 135;
outFile.write((char*)&patient,sizeof(person));
//9
strcpy(patient.personName,"Jane_Doe");
patient.amLevel = 175;
patient.noonLevel = 190;
patient.pmLevel = 215;
outFile.write((char*)&patient,sizeof(person));
//10
strcpy(patient.personName,"Craven_Moorehead");
patient.amLevel = 200;
patient.noonLevel = 175;
patient.pmLevel = 220;
outFile.close();
return 0;
}
So there is all the info that the next program is supposed to read, and here is the read program:
Code:
#include <iostream>
#include <iomanip>
#include <math.h>
#include <ctype.h>
#include <string.h>
#include <fstream>
using namespace std;
void showAll();
void showName();
void showNameLevel();
/***************************************************************
Struct: person
***************************************************************/
struct person
{
char personName[30];
int amLevel;
int noonLevel;
int pmLevel;
};
int main(int argc, char *argv[])
{
ifstream inFile;
person patient[25];
int i = 0;
inFile.open("a:\\Z089933sugar.dat",ios::in|ios::binary);
if(inFile.fail())
{
cout<< "Error! Make sure file exists.";
exit(1);
}
inFile.read((char*)&patient[i],sizeof(person));
while(inFile && i <= 24) // not sure what to do here
{ // might be a for statement?
i++;
}
inFile.close();
switch(argc)
{
case 0:
showAll(); // what should I send
// to these sub-functions?
break;
case 1:
showName();
break;
case 2:
showNameLevel();
break;
default:
cout<< "Too many arguments!";
}
return 0;
}
/*********************************************
Function: showAll
Use:
Arguments:
Returns:
*********************************************/
void showAll()
{
}
/*********************************************
Function: showName
Use:
Arguments:
Returns:
*********************************************/
void showName()
{
}
/*********************************************
Function: showAll
Use:
Arguments:
Returns:
*********************************************/
void showNameLevel()
{
}
I know I should be using the pointer "patient" somehow but I'm not quite sure how to do it, or if I should be building an array or what. Again any help would be appreciated.
Thanks,
Bird