HElp..
Friday 09.19 I must present a little program for my C++ university test.

I remain just a little bug.

I use this code to write my file .DAT

ofstream fileScritturaPartita(nomeFile,ios::binary | ios::out | ios::app);
fileScritturaPartita.write(reinterpret_cast < const char * >(&sfidaDaSalvare),sizeof(Partita));

It works very well but in the object "Partita" there is an array.

I Think above code write on file "Pointers" NOT "value" of the array...

Please How to save values and not pointers???

thanks to everyone helps me pass tih test...

Recommended Answers

All 5 Replies

post the structure or class Partita. We have no clue what you are talking about without that information.

If that structure contains pointers, then you have to write out their values separately, and you will have to design the file format in such a way that the Partita structure and values of all pointers can be read back.

partita.h

pragma once
#include <string>
using std::string;

class Partita
{
public:

	Partita(  unsigned long int = 0, int= 2, unsigned long int[]= 0, double=1 , string="",  bool=true , unsigned long int = 0 ); //costruttore
  
	void setGioco( unsigned long int); 
	unsigned long int getGioco( ) const;

	void setFrequenza( double ); 
	double getFrequenza() const;

	void setNumGiocatori( int ); 
	int getNumGiocatori() const;
	
	void setIdGiocatori( unsigned long int* , int); 
	unsigned long int getIdGiocatori( int ) const;

	void setNote( string) ;
	string getNote() const;

	void setFaceToFace( bool );
	bool getFaceToFace()const;    

	void setIdFile(unsigned long int) ;
	unsigned long int getIdFile()const;    


	//Distruttore
	~Partita(void); //distruttore

private:

	unsigned long int giocoId;
	int numeroGiocatori;
	unsigned long int *idGiocatori ;
	double frequenza;
	char Note [256];
	bool IsFaceToFace;
	unsigned long int fileIdNumember;
	int massimoNumeroGiocatori; //setto a 12 fisso

};

partita.cpp

#include <iostream>
using std::cout;
using std::endl;


#include "Partita.h"
#include "FilePartite.h"

Partita::Partita
	( unsigned long int intIdGioco,int intNumGioc,unsigned long int intIdGioc[], 
		double douFreq, string strNote, bool isFtoF, unsigned long int intIdFile ) 
{
	setGioco( intIdGioco );
	setNumGiocatori( intNumGioc );
	massimoNumeroGiocatori = 12;
	setIdGiocatori( intIdGioc, massimoNumeroGiocatori );
	setFrequenza( douFreq );
	setNote( strNote );
	setFaceToFace( isFtoF );	
	setIdFile( intIdFile );
}


void Partita::setGioco( unsigned long int input)
{
	 giocoId = input;
}

unsigned long int Partita::getGioco() const
{
	return giocoId;
}

//gestione variabile Distributore
void Partita::setNumGiocatori( int input)
{
	numeroGiocatori=input;
}

int Partita::getNumGiocatori() const
{
	return numeroGiocatori;
} 


void Partita::setIdGiocatori( unsigned long int input[],int intNumGioc)
{

	idGiocatori = new unsigned long int[massimoNumeroGiocatori];
	idGiocatori = input ; 

}
unsigned long int Partita::getIdGiocatori(int i) const
{
	return idGiocatori[i];

} 

void Partita::setFrequenza( double input)
{
	frequenza=input;
}

double Partita::getFrequenza() const
{
	return frequenza;
} 


//gestione variabile NOTE
void  Partita::setNote(  string input )
{
	const char *noteValue=input.data();
	int lunghezza = input.length();
	strncpy_s(Note,noteValue,lunghezza);
	Note[lunghezza]='\0'; }

string Partita::getNote() const
{
	return Note;
}


void  Partita::setFaceToFace(  bool input )
{
		IsFaceToFace= input;
}	

bool  Partita::getFaceToFace() const
{
	return IsFaceToFace;
}

//gestione variabile IDfile
void Partita::setIdFile(unsigned long int idFile)
{
fileIdNumember =idFile;


}
unsigned long int Partita::getIdFile() const
{
	return fileIdNumember;
}



//distruttore
Partita::~Partita(void)
{
}

};

If it make simplest..
Array's size is fixed at 12.

I've solved putting 12 variables in the object,

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.