extracting records from a file

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::eek:stream& operator<<( std::eek: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::eek: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>
 
D

Donovan Rebbechi

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.

For a start, I don't understand your format. Is it a fixed byte offset
random access file or a text file ? If you're writing and reading a random
access file, then don't use setw (I don't understand what that's for), use
read() to read a fixed number of bytes, and use write() to write a fixed
number of bytes. If you want a text file, use the std::getline function
which writes and reads a line of the file, and write one field to each line.
Alternatively, you could write one record per line by using a delimiter
character, like a comma ',' but you have to be sure that this character
doesn't appear in any of the names if you do that.

Do not use the stream extraction operator for char* pointers (how do you know
that you allocated enough space ?).

A number of additional points:
(1) does it make sense to change the details of an "appointment" once it's
been created ? Why all the setX methods ?
(2) don't return char* (think for a moment about the memory ownership issues
/problems here). Return a std::string instead.
(3) don't just whine on stderr and keep going if a function fails. Return
an "unsuccesful" error code, or throw an exception.
(4) what are the template params for ? They dont seem to do anything.
(5) don't hard code the filenames

Cheers,
 
K

Karl Heinz Buchegger

Donovan said:
A number of additional points:
(1) does it make sense to change the details of an "appointment" once it's
been created ? Why all the setX methods ?
(2) don't return char* (think for a moment about the memory ownership issues
/problems here). Return a std::string instead.
(3) don't just whine on stderr and keep going if a function fails. Return
an "unsuccesful" error code, or throw an exception.
(4) what are the template params for ? They dont seem to do anything.
(5) don't hard code the filenames

(6) EOF or its usage (despite the fact that the OP used it in a wrong way) has
no use in writing/reading of fixed size record oriented files.
Create an entry, usually at the beginning of the file, and store the number
of records there.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,838
Latest member
KandiceChi

Latest Threads

Top