Ver Mensaje Individual
  #4  
Antiguo 03-09-2013
Avatar de ecfisa
ecfisa ecfisa is offline
Moderador
 
Registrado: dic 2005
Ubicación: Tres Arroyos, Argentina
Posts: 10.508
Reputación: 36
ecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to behold
Cita:
Es decir tengo una variable de una Clase creada por ejemplo TMiPropiaClase esta clase tiene en su interior muchos tipo de datos distintos, se puede guardar de alguna manera el contenido de esta clase en un archivo, base de datos, etc. y después cargarla al abrir el programa nuevamente?
Hola mordaz.

Otra opción es usar los métodos ReadComponent y WriteComponent, lo cuál podes hacer de dos formas:

Derivando tu clase de TComponent encapsulando las funciones y hacíendolas miembros de la primera, como por ejemplo:
Código:
/* TMiClase */
class TMiClase : public TComponent
{
private:
  String FCadena;
  int    FEntero;
  Double FDoble;
__published:  // (solo obtendrás los valores de los miembros publicados)
  __property String Cadena = { read = FCadena, write = FCadena};
  __property int Entero    = { read = FEntero, write = FEntero};
  __property Double Doble  = { read = FDoble,  write = FDoble};
public:
  virtual __fastcall TMiClase(TComponent* Owner);
  void __fastcall SaveToFile(const String aFileName);
  void __fastcall LoadFromFile(const String aFileName);
};

__fastcall TMiClase::TMiClase(TComponent* Owner):TComponent(Owner)
{
  FCadena = "";
  FEntero = 0;
  FDoble  =  0;
}

void __fastcall TMiClase::SaveToFile(const String aFileName)
{
  if (this == NULL)
    throw Exception("Objeto inexistente");
  if (FileExists(aFileName))
    DeleteFile(aFileName);

  TFileStream *FS = new TFileStream(aFileName, fmCreate);
  __try {
   FS->WriteComponent(this);
  }
  __finally {
    delete FS;
  }
}

void __fastcall TMiClase::LoadFromFile(const String aFileName)
{
  if (this == NULL)
    throw Exception("Objeto inexistente");
  if (!FileExists(aFileName))
    throw Exception("Archivo inexistente");

  TFileStream *FS = new TFileStream(aFileName, fmOpenRead);
  __try {
    FS->ReadComponent(this);
  }
  __finally {
    delete FS;
  }
}


/* Form1 */
void __fastcall TForm1::btnSaveClick(TObject *Sender)
{
  TMiClase* MC = new TMiClase(this);

  // Cargar unos datos para prueba...
  MC->Cadena = "Una frase como cualquier otra";
  MC->Entero = 1234;
  MC->Doble  = 3.141592654;

  MC->SaveToFile("C:\\MiCompC.dat");
  delete MC;
}


void __fastcall TForm1::btnReadClick(TObject *Sender)
{
  TMiClase* MC = new TMiClase(this);

  MC->LoadFromFile("C:\\MiCompC.dat");

  // Mostrar
  char *msg = "Cadena: %s \nEntero: %d \nDoble: %8.12f";
  TVarRec vr[] = {MC->Cadena, MC->Entero, MC->Doble};
  ShowMessage(Format(msg,vr,3));

  delete MC;
}
O como funciones fuera de la clase, por ejemplo:
Código:
void SaveComponent(TComponent* CP, const String aFileName)
{
  if (CP == NULL)
    throw Exception("Objeto inexistente");
  if (FileExists(aFileName))
    DeleteFile(aFileName);

  TFileStream *FS = new TFileStream(aFileName, fmCreate);
  __try {
   FS->WriteComponent(CP);
  }
  __finally {
    delete FS;
  }
}

void LoadComponent(TComponent* CP, const String aFileName)
{
  if (CP == NULL)
    throw Exception("Objeto inexistente");
  if (!FileExists(aFileName))
    throw Exception("Archivo inexistente");

  TFileStream *FS = new TFileStream(aFileName, fmOpenRead);
  __try {
    FS->ReadComponent(CP);
  }
  __finally {
    delete FS;
  }
}
Llamadas de ejemplo:
Código:
// Guardar
...
{
  SaveComponent(MC, "C:\\MiCompC.dat");
  ...
  SaveComponent(Label1, "C:\\Label.dat");
}
// Leer
...
{
  LoadComponent(MC, 'C:\\MiCompC.dat');
  //...
  LoadComponent(Label1, "C:\\Label.dat");
}
Saludos.

Edito: Toma en cuenta que sólo funciona para los miembros publicados.
__________________
Daniel Didriksen

Guía de estilo - Uso de las etiquetas - La otra guía de estilo ....

Última edición por ecfisa fecha: 03-09-2013 a las 06:49:34. Razón: comentario
Responder Con Cita