Club Builder - Foros de discusión Club Builder
Foro de C++Builder en español
 
 FAQFAQ   BuscarBuscar   MiembrosMiembros   Grupos de UsuariosGrupos de Usuarios   RegistrarseRegistrarse 
 PerfilPerfil   Entra para ver tus mensajes privadosEntra para ver tus mensajes privados   EntrarEntrar 

de TStringGrid a Archivo

 
Publicar nuevo tema   Responder al tema    Foros de discusión -> Consultas
Ver tema anterior :: Ver tema siguiente  
Autor Mensaje
Mensaje    Asunto: de TStringGrid a Archivo Responder citando

Quisiera saber como puedo guardar el contenido de un TStringGrid en un archivo y posteriormente recuperarlo y mostrarlo en el mismo.

Gracias
Volver arriba
Ver perfil de usuario Enviar mensaje privado Enviar email MSN Messenger
Mensaje    Asunto: Responder citando

Yo haría algo así:
Código:
#include <io.h>
FILE *fString;
AnsiString cTexto;
fString = fopen("String.txt", "ab+");

for (int nRow = 0; nRow < StringGrid1->RowCount; nRow ++)
{
   cTexto = "";
   for (int nCol = 0; nCol < StringGrid1->ColCount; nCol ++)
      cTexto = cTexto + StringGrid1->Cells[nCol][nRow] + "|";

   fprintf(fString, "%s\n", cTexto.c_str());
}
fclose(fString)

Con esto crearías un fichero en el que cada línea se correspondería con cada fila del StrinGrid ylas columans irían separadas por un carácter digamos raro (yo suelo usar el pipe - AltGr + 1, |-).

Para volver a cargar la rejilla, bastaría con ir leyendo el fichero e ir buscano los finales de línea (\n) y los finales de columna (|). Espero que la idea te valga.
_________________
Un saludo

Necesitamos tu ayuda: Envía AI al 27595 (Coste 1,20 euros + IVA)
Volver arriba
Ver perfil de usuario Enviar mensaje privado Enviar email
Mensaje    Asunto: Responder citando

Gracias. Voy a intentar con ese codigo. Apenas soy un novato en el uso de archivos.
Habia utilizado el ejemplo de FileWrite que trae el Builder 5 que guarda mas información y es un poco mas complejo.
Pero lo prefiero mas simple. Gracias.

Código:

The following example uses a button, a string grid, and a Save dialog box on a form. When the button is clicked, the user is prompted for a filename. When the user clicks OK, the contents of the string grid are written to the specified file. Additional information is also written to the file so that it can be read easily with the FileRead function.

#include <dir.h>
void __fastcall TForm1::Button1Click(TObject *Sender)
{
  char szFileName[MAXFILE+4];
  int iFileHandle;
  int iLength;
  if (SaveDialog1->Execute())
  {
    if (FileExists(SaveDialog1->FileName))
    {
      fnsplit(SaveDialog1->FileName.c_str(), 0, 0, szFileName, 0);
      strcat(szFileName, ".BAK");
      RenameFile(SaveDialog1->FileName, szFileName);
    }
    iFileHandle = FileCreate(SaveDialog1->FileName);

    // Write out the number of rows and columns in the grid.
    FileWrite(iFileHandle, (char*)&(StringGrid1->ColCount), sizeof(StringGrid1->ColCount));
    FileWrite(iFileHandle, (char*)&(StringGrid1->RowCount), sizeof(StringGrid1->RowCount));
    for (int x=0;x<StringGrid1->ColCount;x++)
    {
      for (int y=0;y<StringGrid1->RowCount;y++)
      {
        // Write out the length of each string, followed by the string itself.

        iLength = StringGrid1->Cells[x][y].Length();
        FileWrite(iFileHandle, (char*)&iLength, sizeof(iLength));
        FileWrite(iFileHandle, StringGrid1->Cells[x][y].c_str(), StringGrid1->Cells[x][y].Length());
      }
    }
    FileClose(iFileHandle);
  }
}
Volver arriba
Ver perfil de usuario Enviar mensaje privado Enviar email MSN Messenger
Mensaje    Asunto: Responder citando

Pues pregunta sin miedo, que todos hemos sido novatos en algún momento y siempre viene bien ayuda, aunque se tenga experiencia. Smile
_________________
Un saludo

Necesitamos tu ayuda: Envía AI al 27595 (Coste 1,20 euros + IVA)
Volver arriba
Ver perfil de usuario Enviar mensaje privado Enviar email
Mensaje    Asunto: Responder citando

Ok. Muchas Gracias. Ya he mejorado mi trabajo a partir de tu ejemplo.
Seguimos en contacto.
Suerte.
Volver arriba
Ver perfil de usuario Enviar mensaje privado Enviar email MSN Messenger
Mostrar mensajes de anteriores:   
Cambiar a:  
No puedes publicar temas nuevos en este foro
No puedes responder a temas en este foro
No puedes editar tus mensajes en este foro
No puedes borrar tus mensajes en este foro
No puedes votar en las encuestas de este foro


Hosting WordPress Indedmedia desde 3,99 €
© 2002–2026 Club Builder · Funciona con phpBB.