writing data to a file from an array of pointers

This is a discussion on writing data to a file from an array of pointers within the C++ Programming forums, part of the General Programming Boards category; Firstly, I apologized to all who are going to read my program. There are not comments and it is a ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    13

    writing data to a file from an array of pointers

    Firstly, I apologized to all who are going to read my program. There are not comments and it is a long one. Anyway, I have a problem trying to write the data pointed to by Employee * STAFF[20] to a file named employee.dat. It writes the address
    pointed to my STAFF instead. Anyway here is the code

    // the driver file

    Code:
    #include <fstream>
    #include <iomanip>
    #include <cstdlib>
    #include "employee.h"
    using namespace std;
    
    int main()
    {
    
    	Direct D1("Harry Johnson", 25,45365,800,10), D2;
    	Indirect ID3("Larry King", 60, 34546,20,40), ID4;
    
    	Employee *STAFF[20]; cin>>D2; cin>>ID4; system("cls"); 
    	STAFF[0]=&D1; STAFF[1]=&D2;
    	STAFF[2]=&ID3; STAFF[3]=&ID4;
    
    	int count=4;
    
    	ifstream direct; ifstream indirect;
    	direct.open("direct.dat", ios::in);
    	if(direct.fail())
    	{
    		cerr<<"direct.dat cannot be opened\n";
    		exit(1);
    	}
    	indirect.open("indirect.dat", ios::in);
    	if(indirect.fail())
    	{
    		cerr<<"indirect.dat cannot be opened\n";
    		exit(1);
    	}
    
    	while(!direct.eof())
    	{
    		Direct *temp=new Direct;
    		direct>>*temp;
    		STAFF[count]=temp;
    		count++;
    	}
    
    	direct.close();
    	count--;
    
    	while(!indirect.eof())
    	{
    		Indirect *temp=new Indirect;
    		indirect>>*temp;
    		STAFF[count]=temp;
    		count++;
    	}
    
    	indirect.close();
    	count--;
    
    	cout<<"Type"<<setw(11)<<"Name"<<setw(22)<<"Age"<<setw(5)<<"Id"<<setw(17)<<"Basic/Hours"
    		<<setw(11)<<"Cpf/Rate"<<setw(6)<<"Pay"<<endl;
    	cout<<"======================================================"
    		<<"======================\n";
    	
    	for(int loop=0; loop<4; loop++)
    	{
    		STAFF[loop]->cal_pay();
    		STAFF[loop]->display();
    	}
    
    	for(loop=4; loop<count;loop++)
    	{
    		if(loop%8==0)
    		{
    			cout<<endl; system("pause"); system("cls");
    			cout<<"Type"<<setw(11)<<"Name"<<setw(22)<<"Age"<<setw(5)<<"Id"<<setw(17)<<"Basic/Hours"
    		        <<setw(11)<<"Cpf/Rate"<<setw(6)<<"Pay"<<endl;
    
    			cout<<"======================================================"
    				<<"======================\n";
    	
    		}
    		STAFF[loop]->cal_pay();
    		STAFF[loop]->display();
    	}
    
    	system("pause"); system("cls");
    
    	for(loop=1; loop<count; loop++)
    	{
    		for(int number=0; number<count-1; number++)
    		{
    			if(strcmp(STAFF[number]->getname(), 
    				STAFF[number+1]->getname())>0)
    			{
    				Employee *temp=STAFF[number];
    				STAFF[number]=STAFF[number+1];
    				STAFF[number+1]=temp;
    			}
    			
    		}
    	}
    
    	cout<<"Type"<<setw(11)<<"Name"<<setw(22)<<"Age"<<setw(5)<<"Id"<<setw(17)<<"Basic/Hours"
    		<<setw(11)<<"Cpf/Rate"<<setw(6)<<"Pay"<<endl;
    
    	cout<<"======================================================"
    		<<"======================\n";
    	
    
    	for(loop=0; loop<count; loop++)
    	{
    		STAFF[loop]->display();
    	}
    
    	ofstream employee; employee.open("employee.dat", ios::out);
    	if(employee.fail())
    	{
    		cerr<<"employee.dat cannot be opened\n";
    		exit(1);
    	}
    
    	employee<<"Type"<<setw(11)<<"Name"<<setw(22)<<"Age"<<setw(5)<<"Id"<<setw(17)<<"Basic/Hours"
    		<<setw(11)<<"Cpf/Rate"<<setw(6)<<"Pay"<<endl;
    
    	employee<<"======================================================"
    		<<"======================\n";
    
    	for(loop=0; loop<count; loop++)
    	{
                    // how to solve this?
    		employee<<STAFF[loop]<<endl;
    	}
    
    	cout<<endl;
    	return 0;
    }
    
    // the employee.h
    
    #include <iostream>
    using namespace std;
    
    #ifndef _EMPLOYEE_H_
    #define _EMPLOYEE_H_
    
    class Employee
    {
     protected:
    	char name[20]; float age; int EID; float pay;
    public:
    
    	Employee();
    	Employee(char *name, float ag, int ID, float p);
    	char *getname();
    	virtual void cal_pay()=0;
    	virtual void display()=0;
    	
    };
    
    class Direct:public Employee
    {
    	float basic; float cpf_rate;
    public:
    	Direct();
    	Direct(char *name, float ag, int ID, float bas, float rat);
    	void cal_pay();
    	void display();
    	void read_keyboard(istream & input);
    	void read_file(ifstream & input);
    	void write_file(ofstream & output);
    	
    };
    
    istream & operator>>(istream & input, Direct & person);
    ifstream & operator>>(ifstream & input, Direct & person);
    ofstream & operator<<(ofstream & output, Direct & person);
    
    class Indirect:public Employee
    {
    	float hours; float rate;
    public:
    	Indirect();
    	Indirect(char *name, float ag, int ID, float hrs, float rat);
    	void cal_pay();
    	void display();
    	void read_keyboard(istream & input);
    	void read_file(ifstream & input);
    	void write_file(ofstream & output);
    };
    
    istream & operator>>(istream & input, Indirect & person);
    ifstream & operator>>(ifstream & input, Indirect & person);
    ofstream & operator<<(ofstream & output, Indirect & person);
    
    #endif
    
    // the employee.cpp
    
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    #include <cstdlib>
    #include "employee.h"
    
    using namespace std;
    
    Employee::Employee()
    {
    	pay=0;
    }
    
    Employee::Employee(char *nam, float ag, int ID, float p)
    {
    	strcpy(name,nam); age=ag; EID=ID; pay=p; 
    }
    
    char *Employee::getname() 
    {
    	return name;
    }
    
    void Employee::cal_pay()
    {
    
    }
    
    void Employee::display()
    {
    
    }
    
    ////////////////////////////////////////////////////////////////
    
    Direct::Direct()
    {
    	basic=0;
    	cpf_rate=0;
    }
    
    Direct::Direct(char *nam, float ag, int ID, float bas, float rat)
    {
    	strcpy(name,nam); age=ag; EID=ID;
    	basic=bas;
    	cpf_rate=rat;
    }
    
    void Direct::cal_pay()
    {
    	pay=basic*(1-(cpf_rate/100));
    }
    
    void Direct::display()
    {
        int length=strlen(name);
    	cout<<"Direct"<<setw(5+length)<<name;
    
    	char number[10];
    	length=23-length+strlen(itoa(age,number,10));
    	cout<<setw(length)<<age<<setw(9)<<EID; length=3+strlen(itoa(basic,number,10));
    	cout<<setw(length)<<basic; length=14-strlen(itoa(basic,number,10))
    										+strlen(itoa(cpf_rate,number,10));
    	cout<<setw(length)<<cpf_rate;   length=11-strlen(itoa(cpf_rate,number,10))
    									  +strlen(itoa(pay,number,10));
    	cout<<setw(length)<<pay<<endl;
        
    }
    
    void Direct::read_keyboard(istream & input)
    {
    	cout<<"Please enter the name of the employee\n";
    	input.getline(name,255,'\n');
    	cout<<"Please enter the age of the employee\n";
    	input>>age;
    	cout<<"Please enter the id of the employee\n";
    	input>>EID;
    	cout<<"Please enter the basic pay of the employee\n";
    	input>>basic;
    	cout<<"Please enter the cpf rate of the employee\n";
    	input>>cpf_rate;
    	cin.ignore(255,'\n');
    }
    
    void Direct::read_file(ifstream & input)
    {
    	input.getline(name,255,'*'); 
    	input>>age>>EID>>basic>>cpf_rate; input.ignore(255,'\n'); 
    }
    
    void Direct::write_file(ofstream & output)
    {
    	int length=strlen(name);
    	output<<"Direct"<<setw(5+length)<<name;
    
    	char number[10];
    	length=23-length+strlen(itoa(age,number,10));
    	output<<setw(length)<<age<<setw(9)<<EID; length=3+strlen(itoa(basic,number,10));
    	output<<setw(length)<<basic; length=14-strlen(itoa(basic,number,10))
    										+strlen(itoa(cpf_rate,number,10));
    	output<<setw(length)<<cpf_rate;   length=11-strlen(itoa(cpf_rate,number,10))
    									  +strlen(itoa(pay,number,10));
    	output<<setw(length)<<pay<<endl;
    	
    }
    
    istream & operator>>(istream & input, Direct & person)
    {
    	person.read_keyboard(input);
    	return input;
    }
    
    ifstream & operator>>(ifstream & input, Direct & person)
    {
    	person.read_file(input);
    	return input;
    }
    
    ofstream & operator<<(ofstream & output, Direct & person)
    {
    	person.write_file(output);
    	return output;
    }
    
    /////////////////////////////////////////////////////////////////
    
    Indirect::Indirect()
    {
    	hours=0;
    	rate=0;
    }
    
    Indirect::Indirect(char *nam, float ag, int ID, float hrs, 
    				   float rat)
    {
    	strcpy(name,nam); age=ag; EID=ID;
    	hours=hrs;
    	rate=rat;
    }
    
    void Indirect::cal_pay()
    {
    	if(hours>40)
    	{
    		int extra_hours=hours-40;
    		pay=40*rate+extra_hours*1.5*rate;
    	}
    
    	else
    		pay=hours*rate;
    }
    
    void Indirect::display()
    {
    	int length=strlen(name); char number[10];  length=length+3;
    	cout<<"Indirect"<<setw(length)<<name; length=23-strlen(name)
    		                                         +strlen(itoa(age,number,10)); 
    	cout<<setw(length)<<age;
    	cout<<setw(9)<<EID; length=3+strlen(itoa(hours,number,10));
    	cout<<setw(length)<<hours; length=14-strlen(itoa(hours,number,10))
    									  +strlen(itoa(rate,number,10));
    	cout<<setw(length)<<rate; length=11-strlen(itoa(rate,number,10))
    		                             +strlen(itoa(pay,number,10));
    	cout<<setw(length)<<pay<<endl;
    }
    
    void Indirect::read_keyboard(istream & input)
    {
    	cout<<"Please enter the name of the employee\n";
    	input.getline(name,255,'\n');
    	cout<<"Please enter the age of the employee\n";
    	input>>age;
    	cout<<"Please enter the id of the employee\n";
    	input>>EID;
    	cout<<"Please enter the number of hours worked for the employee\n";
    	input>>hours;
    	cout<<"Please enter the rate of the employee\n";
    	input>>rate;
    	cin.ignore(255,'\n');
    }
    
    void Indirect::read_file(ifstream & input)
    {
    	input.getline(name,255,'*'); input>>age>>EID
    		>>hours>>rate; input.ignore(1,EOF);
    	
    }
    
    void Indirect::write_file(ofstream & output)
    {
    	int length=strlen(name); char number[10];  length=length+3;
    	cout<<"Indirect"<<setw(length)<<name; length=23-strlen(name)
    		                                         +strlen(itoa(age,number,10)); 
    	cout<<setw(length)<<age;
    	cout<<setw(9)<<EID; length=3+strlen(itoa(hours,number,10));
    	cout<<setw(length)<<hours; length=14-strlen(itoa(hours,number,10))
    									  +strlen(itoa(rate,number,10));
    	cout<<setw(length)<<rate; length=11-strlen(itoa(rate,number,10))
    		                             +strlen(itoa(pay,number,10));
    	cout<<setw(length)<<pay<<endl;
    	
    }
    
    istream & operator>>(istream & input, Indirect & person)
    {
    	person.read_keyboard(input);
    	return input;
    }
    
    ifstream & operator>>(ifstream & input, Indirect & person)
    {
    	person.read_file(input);
    	return input;
    }
    
    ofstream & operator<<(ofstream & output, Indirect & person)
    {
    	person.write_file(output);
    	return output;
    }
    Here is the output of employee.dat

    Type Name Age Id Basic/Hours Cpf/Rate Pay
    ================================================== ==========================
    004A42F0
    0012FF1C
    004A4410
    004A33A0
    004A3340
    004A34F0
    004A4350
    004A41D0
    0012FF48
    004A4290
    0012FEF0
    0012FEC4
    004A4230
    Last edited by Mingzhi; 07-19-2004 at 08:28 AM.

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    In the follow code you want to copy the string from classes Direct and Indirect to class STAFF, correct?

    STAFF[0]=&D1;
    STAFF[1]=&D2;
    STAFF[2]=&ID3;
    STAFF[3]=&ID4;

    You are assigning each element of STAFF array the address of the first element of the other instantiations. One solution is strcpy(). Another solution is memcpy().

    Kuphryn

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  2. Replies: 1
    Last Post: 09-10-2005, 06:02 AM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21