PDA

View Full Version : [C++] Lettura e scrittura da/su file binari


dlbp
03-02-2011, 12:20
Salve a tutti....
sono due giorni che sbatto la testa sull'argomento della lettura e scrittura su file binari.
Ho una traccia che mi richiede di implementare una classe Pratica e di ridefinire gli operatori >> e << per la lettura e scrittura da/su file binari.

Per iniziare ho dichiarato friend della classe le seguenti funzioni

friend istream& operator >> (istream&,Pratica&);
friend ostream& operator << (ostream&,const Pratica&);
friend ofstream& operator << (ofstream&,Pratica &);
friend ifstream& operator >> (ifstream&,Pratica&);

gli operatori di >> e << del cin e cout non mi hanno dato problemi....

Invece quelli per i file mi stanno dando del file da torcere

La traccia inoltre mi richiede che nella scrittura su file si deve tener conto solamente dei caratteri presenti nella stringa.

per l'operator << per la scrittura su file binario l'ho implementata cosi:

ofstream& operator << (ofstream& file,Pratica&P){
file.open("salva.txt",ios::out|ios::binary);
file.write(((P.getNome())),strlen(P.getNome()));
file.write(((P.getCognome())),strlen(P.getCognome()));
file.close();
return file;
}

E' fatto bene questo?

Per quanto riguarda l'operator >> per la lettura da file binario non riesco proprio a muovermi...chiedo una vostra mano che sicuramente ne sapete di pił...mettiamo il caso che voglio leggere da file binario una pratica che ho gią scritto su file...come devo fare??

Vi posto anche la classe Pratica:

HEADER FILE

#ifndef _PRATICA_H_
#define _PRATICA_H_
#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;

class Pratica{
friend istream& operator >> (istream&,Pratica&);
friend ostream& operator << (ostream&,const Pratica&);
friend ofstream& operator << (ofstream&,Pratica &);
friend ifstream& operator >> (ifstream&,Pratica &);
protected:
char* nome;
char* cognome;


public:
Pratica(const char* ="",const char* ="");
~Pratica();
Pratica(const Pratica&);
Pratica& operator = (const Pratica&);
virtual ostream& stampa(ostream&) const;
char* getNome(){return nome;}
const char* getCognome(){return cognome;}
};


#endif

IMPLEMENTAZIONE

#include "pratica.h"

Pratica::Pratica(const char* N,const char* C){
nome=new char[strlen(N)+1];
strcpy(nome,N);
cognome=new char[strlen(C)+1];
strcpy(cognome,C);
}

Pratica::~Pratica(){
delete[] nome;
delete[] cognome;
}

Pratica::Pratica(const Pratica&P){
this->nome=new char[strlen(P.nome)+1];
strcpy(this->nome,P.nome);
this->cognome=new char[strlen(P.cognome)+1];
strcpy(this->cognome,P.cognome);
}

Pratica& Pratica::operator=(const Pratica&P){
if(this!=&P){
delete[] nome;
delete[] cognome;
nome=new char[strlen(P.nome)+1];
strcpy(nome,P.nome);
cognome=new char[strlen(P.cognome)+1];
strcpy(cognome,P.cognome);
}
return (*this);
}

istream& operator >> (istream& in,Pratica&P){
char buffer[20];
delete[] P.nome;
delete[] P.cognome;
cout<<"Inserire nome: ";
in.getline(buffer,19);
P.nome=new char[strlen(buffer)+1];
strcpy(P.nome,buffer);
cout<<"Inserire cognome: ";
in.getline(buffer,19);
P.cognome=new char[strlen(buffer)+1];
strcpy(P.cognome,buffer);
return in;
}

ostream& operator << (ostream& out,const Pratica&P){
out<<"DETTAGLI PRATICA\n";
out<<"NOME: "<<P.nome<<endl;
out<<"COGNOME: "<<P.cognome<<endl;

return out;
}

ostream& Pratica::stampa(ostream& out) const{
out<<"DETTAGLI PRATICA\n";
out<<"NOME: "<<nome<<endl;
out<<"COGNOME: "<<cognome<<endl;
return out;
}


ofstream& operator << (ofstream& file,Pratica&P){
file.open("salva.txt",ios::out|ios::binary);
file.write(((P.getNome())),strlen(P.getNome()));
file.write(((P.getCognome())),strlen(P.getCognome()));
file.close();
return file;
}

Aiutatemi....vi prego