Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > extracting records from a file

Reply
Thread Tools

extracting records from a file

 
 
Aleander
Guest
Posts: n/a
 
      05-29-2005
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[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>


 
Reply With Quote
 
 
 
 
Donovan Rebbechi
Guest
Posts: n/a
 
      05-29-2005
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/
 
Reply With Quote
 
 
 
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      05-30-2005
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
extracting file from the file descriptor akaarj@rediffmail.com C++ 3 06-15-2007 09:16 PM
Simple query returns 0 records in asp, but all records in vbscript masg0013@gmail.com ASP General 3 11-02-2006 09:23 AM
Delete records or update records Dan ASP General 1 05-10-2004 01:25 PM
match muliple header records to associated detail records Luke Airig XML 0 12-31-2003 12:06 AM
Extracting records Dj Frenzy Perl 1 12-02-2003 11:01 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57