By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,918 Members | 1,852 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,918 IT Pros & Developers. It's quick & easy.

extracting records from a file

P: n/a
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::ostream& operator<<( std::ostream& 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::out);
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[i].getNome()
<< setw(30) << diario[i].getCognome()
<< setw(20) << diario[i].getTelefono()
<< diario[i].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
setw(30) >> cog
setw(20) >> tel
data;


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>
Jul 23 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On 2005-05-29, Aleander <> wrote:
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,
--
Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord/
Jul 23 '05 #2

P: n/a
Donovan Rebbechi wrote:

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.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.