i compile this code in Microsoft visual studio C++. No errors.. but i cant save the file.. why ??

#include <iostream>
#include <vector>
#include <fstream>
#include <conio.h>
using namespace std;
using std::vector;

class Service {
public:
   Service( string d ) { strcpy(servDate,d.c_str()); }
   void setDate(string d) { strcpy(servDate,d.c_str()); }
   string getDate() { return servDate; }
   virtual double GetServiceCharge() { return 0; }
protected:
    char servDate[100];
};

class Regular : public Service {
public:
   Regular( string d ) : Service(d) {}
   double GetServiceCharge() { return 100; }
};

class Special : public Service {
public:
   Special( string d ) : Service(d) {}
   double GetServiceCharge() { return 200; }
};

void saveFile(vector<Service*> newVecService)
{
    int i = 0;
    fstream file;

    file.open("service.txt",ios::out|ios::trunc|ios::binary);
    cout<<"tes";getch();

    for(i=0; i < newVecService.size(); i++)
    {
        if(newVecService[i] != NULL)
        {
            file.write((char*)&(*newVecService[i]), sizeof(*newVecService[i]));
        }
    }
   file.close();
}

int main() {
    Service *newServ;
    vector<Service*> vecService(300,newServ);
    
    newServ = new Regular("12/12/2007");
    vecService[1] = newServ;                // add data to vector

    saveFile(vecService);
    return 0;
}

thanks in advance..

>> file.write((char*)&(*newVecService), sizeof(*newVecService));

try this: file.write((char*)newVecService[i], sizeof(*newVecService[i])); newVecService is alread a pointer -- no need for all that typecasting.

>>void saveFile(vector<Service*> newVecService)
pass the vector by reference to avoid duplicating the vector void saveFile(vector<Service*>& newVecService) >>Service *newServ;
>>vector<Service*> vecService(300,newServ);

You are initializing each of the 300 vector elements with an uninitialized pointer. Initialize the pointer to 0 first Service *newServ = 0;

ancient dragon, thanks you.. i am happy with your reply :)

the value was really saved inside the 'service.txt'.. but it is unreadable..

and then i try to add a readFile code that shown at below :

vector<Service*> readFile(vector<Service*>& newVecService, Service *newServ)
{
	int i = 0;
	fstream file;

	file.open("service.txt",ios::in|ios::binary);

	if(file.is_open())
	{
		file.read((char*)&newServ, sizeof(newServ));
		while(!file.eof())
		{
			newVecService[i] = newServ;
			file.read((char*)&newServ, sizeof(newServ));
			if(newVecService[i] != NULL)
			{
				++i;
			}
		}
	}
	return newVecService;
}

int main() {
	Service *newServ = 0;
	vector<Service*> vecService(300,newServ);

	newServ = new Regular("12/12/2007");
	vecService[1] = newServ;				// add data to vector

	saveFile(vecService);
	
	vecService = readFile(vecService, newServ);
	return 0;
}

could you point me in right direction ??

make sure you understand the anciant dragon's post completely :).

your algorithm doesn't do the right thing in reading

it should read after opening file directly
remove your first read call

one more thing return the vector<>& from readFile function
replace the file.read (...) , problem lies there.
you will solve it

file.read((char*)&newServ, sizeof(newServ)); That line won't work for two reasons

  • &newServ -- newServ is already a pointer as declared in the function parameter. So what you are creating with &newServ is a pointer to a pointer, which is not what you want. Remove the & pointer operator.
  • sizeof(newServ)); -- this is taking the size of a pointer, and on 32-bit compilers the size of any pointer is 4. What you want is the size of the Service class. There are two ways you can fix this and both are correct so its up to you which you want. 1) sizeof( *newServ ) or sizeof( Service )

>>while(!file.eof())
What is not the correct way to write that loop because eof() doesn't work that way. Here is how to do it

newServ = new Service; // allocate memory for the read function  
i = 0;
while( file.read((char*)newServ, sizeof(*newServ)) )
{
    newVecService[i] = newServ;
   newServ = new Service; // allocate memory for the read function  
   i++;
}
delete newServ;
newServ = 0;

to: Laiq ahmed, i am not really good in this pointer thing... Moreover i am just a beginner in C++.. :$

Anyway, i should say thanks to you guys for your all guidance(especially ancient dragon)..

THANK YOU.. :)

ancient dragon.. i need your help again.. :)

last time the my problem solved..
but now i want to add an attribute for this abstract class Service :

class Service {
public:
   Service( string d ) { strcpy(servDate,d.c_str()); }
protected:
    char servDate[100];
    vector<char> newVecChar; // <--- new attribute

what should i do in method saveFile & readFile in order to save and read the file properly?

You dont need to change anything in those functions. where are you populating this vector?

chandra, is your the populating means listing the vector ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.