Visualizzazione dei risultati da 1 a 6 su 6
  1. #1

    [C++] overloading operatori di shift per file binario

    Salve a tutti stavo realizzando un programma che vuole gestire i dati relativi ad un libro; i dati sono: autore(char*),titolo(char*) e anno(int). Oltre ai costruttori e distruttori e alle funzioni get si vogliono realizzare l'oveloading degli operatori di shift ( << e >>) per l'inserimento e la lettura su file binario; Ho realizzato tutto quanto ma il programma mi crasha.Provando ad eseguire step by step forse c'è un problema nell'overloading di << ma non sono sicurissimo.Qualcuno può aiutarmi?
    codice:
    #ifndef LIBRO_H_
    #define LIBRO_H_
    #include <fstream>
    #include <iostream>
    #include <cstring>
    
    using namespace std;
    
    class libro{
          
          private:
                  char* autore;
                  char* titolo;
                  int anno;
                  
          public:
        libro(const char* a="",const char* t="",const int aa=0): autore(new char[strlen(a)+1]), titolo(new char[strlen(t)+1]), anno(aa) {strcpy(autore,a); strcpy(titolo,t); anno=aa;}
                 libro(const libro &);
                 ~libro() {delete[] autore; delete[] titolo;}
                 const libro & operator=(const libro &);
                 const char* get_autore()const {return autore;}
                 const char* get_titolo()const {return titolo;}
                 int get_anno()const {return anno;}
                 friend ofstream& operator<<(ofstream&, libro&);
                 friend ifstream& operator>>(ifstream&,libro&);
                 friend ostream& operator<<(ostream&,const libro&);
          
          };
    #endif
    
    #include "libro.h"
    
    libro::libro(const libro & L)
    {
     autore=new char[strlen(L.autore)+1];
     strcpy(this->autore,L.autore);
     titolo=new char[strlen(L.titolo)+1];
     strcpy(this->titolo,L.titolo);
     anno=L.anno;
    }
    
    ofstream& operator<<(ofstream& ofs, libro& l)
    {
        int size=strlen(l.autore);
        ofs.write(reinterpret_cast<char*>(&size), sizeof(int));
        ofs.write(l.autore, size*sizeof(char));
        
        size=strlen(l.titolo);
        ofs.write(reinterpret_cast<char*>(&size), sizeof(size));
        ofs.write(l.titolo, size*sizeof(char));
        
        ofs.write(reinterpret_cast<const char*>(&l.anno),sizeof(int));
        return ofs;
    }
    
    ifstream& operator>>(ifstream& ifs,libro& l)
    {
        int size=0;
        
        ifs.read(reinterpret_cast<char*>(&size), sizeof(int));
        ifs.read(l.autore, size*sizeof(char));
        
        ifs.read(reinterpret_cast<char*>(&size), sizeof(int));
        ifs.read(l.titolo, size*sizeof(char));
        
        ifs.read(reinterpret_cast<char*>(&l.anno), sizeof(int));
        
        return ifs;
    }
    
    ostream& operator<<(ostream& os,const libro & L)
    {
     os<<"\nAutore: " << L.autore<< "\nTitolo: "<< L.titolo << "\nAnno pubblicazione: " << L.anno <<"\n";
     return os;
    }
    #include <cstdlib>
    #include <iostream>
    #include "libro.h"
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        libro l("Dan Brown", "Il Codice da Vinci", 2005);
        
        ofstream out("dati.dat", ios::binary);
        out<<l;
        out.close();
        
        libro p;
        ifstream in("dati.dat", ios::binary);
        in>>p;
        in.close();
        
        cout << p;
        system("PAUSE");
        return 0;
    }

  2. #2
    Utente di HTML.it L'avatar di shodan
    Registrato dal
    Jun 2001
    Messaggi
    2,381
    operator << mi pare a posto.

    E' operator >> che è da sistemare.
    Lo spazio effettivo che l.autore possiede è al massimo 1 byte. Rivedi il costruttore.
    (E non solo l.autore ovviamente)
    This code and information is provided "as is" without warranty of any kind, either expressed
    or implied, including but not limited to the implied warranties of merchantability and/or
    fitness for a particular purpose.

  3. #3
    parli del costruttore con i parametri di default che non va?

  4. #4
    (per inciso, secondo me non è una buona idea fare l'overload di << e >> per IO binario - tipicamente vengono usati per IO formattato)
    Amaro C++, il gusto pieno dell'undefined behavior.

  5. #5
    Utente di HTML.it L'avatar di shodan
    Registrato dal
    Jun 2001
    Messaggi
    2,381
    Si.
    Se effettui una strlen() su autore in operator >> ti accorgi che vale 1.
    Questo perché nel costruttore allochi memoria per una stringa vuota.
    Un approccio corretto sarebbe definire un costruttore di default che azzeri i vari puntatori e togliere i parametri di default nel costruttore parametrico.
    All'interno di operator >> effettui un check di validità dei puntatori (sono NULL? Non sono NULL?) e ti regoli di conseguenza.
    This code and information is provided "as is" without warranty of any kind, either expressed
    or implied, including but not limited to the implied warranties of merchantability and/or
    fitness for a particular purpose.

  6. #6
    Per MItaly: in effetti hai ragione ma purtroppo la prof in qualche prova d'esame lo mette e quindi bisogna accontentarla ; per shodan: ok seguirò le tue linee guida. E comunque un grazie a tutti per la vostra disponibilità

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.