extracting records from a file

Discussion in 'C++' started by Aleander, May 29, 2005.

  1. Aleander

    Aleander Guest

    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
    >> 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>
    Aleander, May 29, 2005
    #1
    1. Advertising

  2. 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/
    Donovan Rebbechi, May 29, 2005
    #2
    1. Advertising

  3. 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
    Karl Heinz Buchegger, May 30, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dj Frenzy

    Extracting records

    Dj Frenzy, Dec 2, 2003, in forum: Perl
    Replies:
    1
    Views:
    438
    Gunnar Hjalmarsson
    Dec 2, 2003
  2. Luke Airig
    Replies:
    0
    Views:
    765
    Luke Airig
    Dec 31, 2003
  3. Replies:
    3
    Views:
    294
    Ron Natalie
    Jun 15, 2007
  4. Dan

    Delete records or update records

    Dan, May 10, 2004, in forum: ASP General
    Replies:
    1
    Views:
    449
    Ray at
    May 10, 2004
  5. Replies:
    3
    Views:
    638
    Anthony Jones
    Nov 2, 2006
Loading...

Share This Page