 |
Club Builder Foro de C++Builder en español
|
| Ver tema anterior :: Ver tema siguiente |
| Autor |
Mensaje |
vikingo Cdt


Registrado: 16 Ago 2004Mensajes: 5
|
Publicado: Lun 16 Ago, 2004 8:02 am Asunto: de TStringGrid a Archivo |
|
|
Quisiera saber como puedo guardar el contenido de un TStringGrid en un archivo y posteriormente recuperarlo y mostrarlo en el mismo.
Gracias |
|
| Volver arriba |
|
 |
Angel Master


Registrado: 15 Abr 2003Mensajes: 2311Ubicación: Toledo (España)
|
Publicado: Lun 16 Ago, 2004 12:28 pm Asunto: |
|
|
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 |
|
 |
vikingo Cdt


Registrado: 16 Ago 2004Mensajes: 5
|
Publicado: Lun 16 Ago, 2004 9:31 pm Asunto: |
|
|
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 |
|
 |
Angel Master


Registrado: 15 Abr 2003Mensajes: 2311Ubicación: Toledo (España)
|
|
| Volver arriba |
|
 |
vikingo Cdt


Registrado: 16 Ago 2004Mensajes: 5
|
Publicado: Mar 17 Ago, 2004 9:19 pm Asunto: |
|
|
Ok. Muchas Gracias. Ya he mejorado mi trabajo a partir de tu ejemplo.
Seguimos en contacto.
Suerte. |
|
| Volver arriba |
|
 |
|
|
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
|
© 2002–2026 Club Builder · Funciona con phpBB.
|