A
Aleander
Hi!
I have to write the records of a vector in a file, e and then open this file
to extract the record to refill the vector.
My program has two class: Visita(Appointment) and Data(date). The second one
is used in the first one such as a field. I wrote the scrivi() function to
write the record in a file called "archivio.txt" and it works fine.
The I wrote the carica() function to load the records that I have placed on
the file, but I don't know what I have mistaken.
In debugging I saw that the while loop "while( ifs.get() != EOF )", that I
used to read the content of the file untill the end, stops after only one
step. In fact my after calling the function carica() I have only a record in
my vector "diario".
Can someone help me?
I have copy pasted a part of the code, if you need more, tell me.
tnx a loooooooot...
ps. Sorry for my bad english.
--
Aleander
<code>
/* CLASS Visita */
class Visita {
friend std::istream& operator>>( std::istream& in, Visita& v);
friend std:stream& operator<<( std:stream& out, const Visita& v);
public:
Visita(string n = "", string c= "", string t= "", Data r =
Data(01,01,1900) )
: nome(n), cognome(c), telefono(t), dataApp(r) {};
Visita( const Visita &v );
char* str(); // converte un oggetto visita in uno char*
bool operator<(Visita &v);
string getNome() const { return nome; }
string getCognome() const { return cognome; }
string getTelefono() const { return telefono; }
Data getData() const { return dataApp; }
Visita& setNome(string n){ nome=n; return *this; }
Visita& setCognome(string c){ cognome=c; return *this; }
Visita& setTelefono(string t){ telefono=t; return *this; }
Visita& setData(int g,int m,int a){ dataApp=Data(g,m,a); return
*this; }
// private:
string nome;
string cognome;
string telefono;
Data dataApp;
};
// CLASS Data
class Data{
friend ostream& operator<<( ostream& output, const Data &d);
friend istream& operator>>( istream& input, Data &d);
friend class Visita;
public:
Data(int g=3, int m=3, int a=1903); // costruttore
Data( const Data &d);
Data& operator=( const Data &r );
int getGiorno() const { return giorno; } //funzioni di utilità get
data
int getMese() const { return mese; }
int getAnno() const { return anno; }
private:
int giorno;
int mese;
int anno;
};
// FUNCTION THAT SAVES ALL ON A FILE
template<class T>
void salva(){
ofstream ofs("archivio.txt", ios:ut);
if (!ofs)
cerr << "Impossibile aprire file!";
ofs.seekp(0); // si posiziona all'inizio del file
for (int i=0; i< diario.size(); i++){
ofs.seekp(i*91);
ofs.setf( ios::left );
ofs << setw(30) << diario.getNome()
<< setw(30) << diario.getCognome()
<< setw(20) << diario.getTelefono()
<< diario.getData()
<< '\n';
}
ofs.close();
}
/* FUNCTION THAT LOAD THE FILE
template<class T>
void carica(){
ifstream ifs("archivio.txt", ios::in );
if ( !ifs ){
cerr << "File could not be opened." << endl;
system("PAUSE");
mostraMenu();
}else{
diario.clear();
char nom[30];
char cog[30];
char tel[20];
Data data;
int i=0;
while( ifs.get() != EOF ){
ifs.seekg(i*91);
ifs >> setw(30) >> nom
Visita buffer;
buffer.setNome(nom);
buffer.setCognome(cog);
buffer.setTelefono(tel);
buffer.setData( data.getGiorno(), data.getMese(), data.getAnno() );
cout << "Caricato:\n" << buffer << endl;
diario.push_back(buffer);
i++;
}
}
system("PAUSE");
}
</code>
I have to write the records of a vector in a file, e and then open this file
to extract the record to refill the vector.
My program has two class: Visita(Appointment) and Data(date). The second one
is used in the first one such as a field. I wrote the scrivi() function to
write the record in a file called "archivio.txt" and it works fine.
The I wrote the carica() function to load the records that I have placed on
the file, but I don't know what I have mistaken.
In debugging I saw that the while loop "while( ifs.get() != EOF )", that I
used to read the content of the file untill the end, stops after only one
step. In fact my after calling the function carica() I have only a record in
my vector "diario".
Can someone help me?
I have copy pasted a part of the code, if you need more, tell me.
tnx a loooooooot...
ps. Sorry for my bad english.
--
Aleander
<code>
/* CLASS Visita */
class Visita {
friend std::istream& operator>>( std::istream& in, Visita& v);
friend std:stream& operator<<( std:stream& out, const Visita& v);
public:
Visita(string n = "", string c= "", string t= "", Data r =
Data(01,01,1900) )
: nome(n), cognome(c), telefono(t), dataApp(r) {};
Visita( const Visita &v );
char* str(); // converte un oggetto visita in uno char*
bool operator<(Visita &v);
string getNome() const { return nome; }
string getCognome() const { return cognome; }
string getTelefono() const { return telefono; }
Data getData() const { return dataApp; }
Visita& setNome(string n){ nome=n; return *this; }
Visita& setCognome(string c){ cognome=c; return *this; }
Visita& setTelefono(string t){ telefono=t; return *this; }
Visita& setData(int g,int m,int a){ dataApp=Data(g,m,a); return
*this; }
// private:
string nome;
string cognome;
string telefono;
Data dataApp;
};
// CLASS Data
class Data{
friend ostream& operator<<( ostream& output, const Data &d);
friend istream& operator>>( istream& input, Data &d);
friend class Visita;
public:
Data(int g=3, int m=3, int a=1903); // costruttore
Data( const Data &d);
Data& operator=( const Data &r );
int getGiorno() const { return giorno; } //funzioni di utilità get
data
int getMese() const { return mese; }
int getAnno() const { return anno; }
private:
int giorno;
int mese;
int anno;
};
// FUNCTION THAT SAVES ALL ON A FILE
template<class T>
void salva(){
ofstream ofs("archivio.txt", ios:ut);
if (!ofs)
cerr << "Impossibile aprire file!";
ofs.seekp(0); // si posiziona all'inizio del file
for (int i=0; i< diario.size(); i++){
ofs.seekp(i*91);
ofs.setf( ios::left );
ofs << setw(30) << diario.getNome()
<< setw(30) << diario.getCognome()
<< setw(20) << diario.getTelefono()
<< diario.getData()
<< '\n';
}
ofs.close();
}
/* FUNCTION THAT LOAD THE FILE
template<class T>
void carica(){
ifstream ifs("archivio.txt", ios::in );
if ( !ifs ){
cerr << "File could not be opened." << endl;
system("PAUSE");
mostraMenu();
}else{
diario.clear();
char nom[30];
char cog[30];
char tel[20];
Data data;
int i=0;
while( ifs.get() != EOF ){
ifs.seekg(i*91);
ifs >> setw(30) >> nom
Visita buffer;
buffer.setNome(nom);
buffer.setCognome(cog);
buffer.setTelefono(tel);
buffer.setData( data.getGiorno(), data.getMese(), data.getAnno() );
cout << "Caricato:\n" << buffer << endl;
diario.push_back(buffer);
i++;
}
}
system("PAUSE");
}
</code>