Re-running program! displayed unreadable charactes.. HELP PLS

This is a discussion on Re-running program! displayed unreadable charactes.. HELP PLS within the C++ Programming forums, part of the General Programming Boards category; hi all guru, here, my problem is re-running the program, the vector attribute of Service Class was listed unreadable characters.. ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    2

    Re-running program! displayed unreadable charactes.. HELP PLS

    hi all guru,

    here, my problem is re-running the program, the vector attribute of Service Class was listed unreadable characters.. WHY??

    Below is my following code :

    Service.h code:
    Code:
    class Service{
    protected:
    	char	servType[100];
    	vector<Medicine> vecMedicine; // the purpose that i created a vector attribute is a service can have many medicine
    	vector<vector<char> > vecDosage; 
    public:
    	static int servNo;
    public:
    	Service() 
    	{
    		servNo++;
    	}
    	
    	//set
    	void setServType(string newServType) { strcpy(servType, newServType.c_str()); }
    	void setMedicine(Medicine newMedicine) { vecMedicine.push_back(newMedicine); }
    	void setDosage(vector<char> newVecDosage) { vecDosage.push_back(newVecDosage); }
    
    	//get
    	string getServType() { return servType; }
    	vector<Medicine> getMedicine() { return vecMedicine; }
    	vector<vector<char> > getDosage() { return vecDosage; }
    
    	virtual double getServiceCharge() { return 0; }
    Regular.h code:
    Code:
    #define REGULAR_CHARGE 50;
    
    class Regular : public Service{
    public:
    	Regular() { strcpy(servType,"Regular"); }
    
    	double getServiceCharge() { return REGULAR_CHARGE; }
    };
    serviceDAO.h code:
    Code:
    #define SERVICE_FILE "db/service.txt"
    
    class ServiceDAO{
    public:
    	ServiceDAO() { }	
    	void saveFile(vector<Service*>& newVecService)
    	{
    		int i = 0;
    		fstream file;
    
    		file.open(SERVICE_FILE,ios::out|ios::trunc|ios::binary);
    		
    		for(i=0; i < newVecService.size(); i++)
    		{
    			if(newVecService[i] != NULL)
    			{
    				file.write((char*)newVecService[i], sizeof(*newVecService[i]));
    			}
    		}
    	}
    
    	vector<Service*>& readFile(vector<Service*>& newVecService, Service *newServ)
    	{
    		int i = 1;
    		newServ = new Service; // allocate memory for the read function  
    		fstream file;
    
    		--Service::servNo;
    		file.open(SERVICE_FILE,ios::in|ios::binary);
    
    		if(file.is_open())
    		{
    			while(file.read((char*)newServ, sizeof(*newServ)))
    			{
    				newVecService[i] = newServ;
    				newServ = new Service; // allocate memory for the read function  
    				if(newVecService[i] != NULL)
    				{
    					Service::servNo = i;
    				}
    				++i;
    			}
    			delete newServ;
    			newServ = 0;
    		}
    		return newVecService;
    	}
    };
    Main.cpp code:
    Code:
    static const int MAXIMUM_RECORDS = 300;
    int Service::servNo = 0;
    
    Service *newServ = 0;
    ServiceDAO servDAO;
    vector<Service*> vecService;
    
    void main()
    {
    	vecService.assign(MAXIMUM_RECORDS, newServ);
    	vecService = servDAO.readFile(vecService, newServ);
    	do
    	{
    		// at here display the main menu
    		// switch(choice)
    		// case 1 goto addService();break;
    		// case 2 goto searchService();break;
    		// case 3 goto listService();break;
    	}
    	servDAO.saveFile(vecService);
    }
    
    void addService()
    {
    	newServ = new Regular();
    	vecService[Service::servNo] = newServ;
    	// back to main menu
    }
    
    void searchService()
    {
    	newServ = new Regular();
    	
    	// enter a valid medicine input
    	vecService[x]->setMedicine(vecMedicine[iPosMed]);
    	
    	// enter a valid dosage input
    	// pusback the value into tempVec
    	vector<char> tempVec;
    	vecService[pos]->setDosage(tempVec);
    
    	// back to main menu
    }
    
    void listService()
    {
    	system("cls");
    	int x;
    	int z = 0;
    	int iMed = 0;
    	string temp;
    	gotoxy(0,8);cout << "No.";
    	gotoxy(5,8);cout << "Type ";
    	gotoxy(55,8);cout << "Medicine(s)";
    	gotoxy(0,9);cout << "-------------------------------------------------------------------------------";
    	for (x=1;x<vecService.size();x++)
    	{
    		if(vecService[x] != NULL)
    		{
    			gotoxy(0,9+x+z);cout << x;
    			gotoxy(5,9+x+z);cout << vecService[x]->getServType().c_str();
    			if(vecService[x]->getMedicine().size() != 0)
    			{
    				for(iMed = 0; iMed < vecService[x]->getMedicine().size(); iMed++)
    				{
    					if(iMed == 0)
    					{
    						temp = vecService[x]->getMedicine()[iMed].getMedName();
    					}
    					else
    					{
    						temp += ", " + vecService[x]->getMedicine()[iMed].getMedName();
    					}
    					gotoxy(55,9+x+z);cout << temp.c_str(); 
    				}
    			}
    			else
    			{
    				gotoxy(55,9+x+z);cout << "<<empty>>";
    			}
    			
    		}
    		else
    		{
    			--z;
    		}
    	}
    }
    hope all guru can help me solve my problem..

    Thanks in advance..

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You mean, why saving and reading the file doesn't work.

    Code:
    file.write((char*)newVecService[i], sizeof(*newVecService[i]));
    Well good luck. The Service class contains std::vectors, so you've just written a few memory addresses to the file. Sadly, you cannot reconstruct the vectors from that data.

    You'll need to write a function that actually writes the service's data into the file.

    Aside from that your code seems to go about memory quite recklessly, and it is curious why you us MAXIMUM_RECORDS = 300 (together with an awkward attempt to store the service count in the service class) and why you just don't push_back new services as they come.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    2
    i am very grateful and thanks anon for your time to reply my thread..

    I am not sure whether my saveFile() method and readFile() method code are correct..

    the problem is when i call listService() method, the std::vector of the Service class was listed in unreadable characters...

    anon, i need your guidance. what should i do ??
    And what you mean by writing a function that actually writes the service's data into the file??

    all guru please help me to solve this..

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Yes, you'll need to write the actual data to a file, in a way that lets you later reconstruct the data structures.

    A vector basically contains a few pointers to dynamically allocated memory. Overwriting its bit pattern (with reinterpret_cast) most certainly won't result in a valid vector object.

    Anyway, why don't you use std::string instead of char arrays. You probably won't be able to save the Service data in a binary format anyway.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 02:42 AM
  2. Running program in unix
    By Cpro in forum Linux Programming
    Replies: 2
    Last Post: 02-10-2008, 09:28 PM
  3. Running Program Problem
    By warfang in forum C++ Programming
    Replies: 10
    Last Post: 03-28-2007, 03:02 PM
  4. Running another program in windows
    By Mr_Jack in forum C++ Programming
    Replies: 4
    Last Post: 01-22-2004, 02:01 AM

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