Tfilestream en c++ builder

Bueno mi pregunta es la siguiente quiero manipular archivos binarios, con el compilador de borland conocido como c++ builder. El problema es que no se como instanciar la clase Tfilestream, mi pregunta es si tu sabes o conoces como poder manejar archivos usando esta clase. Si no es mucha molesttia podría darme un pequeño ejemplo.

1 Respuesta

Respuesta
1
Espero que te sirva esto:
1.- Crea un proyecto nuevo.
2.- Sobrescribe el contenido de Unit1. Dfm. Esto se logra dando click derecho sobre la forma Unit1 y luego en el menu selecciona "View as Text". Ojo, debes borrar todo lo que tenga por default y solo dejar el código de abajo.
//------------------------------------------------------------------------
//UNIT1.DFM DESDE AQUI (NO COPIES ESTO)
//------------------------------------------------------------------------
object Form1: TForm1
Left = 192
Top = 104
Width = 545
Height = 246
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object Label1: TLabel
Left = 24
Top = 80
Width = 37
Height = 13
Caption = 'Nombre'
end
object Label2: TLabel
Left = 24
Top = 112
Width = 48
Height = 13
Caption = 'Direccion:'
end
object Label3: TLabel
Left = 24
Top = 16
Width = 39
Height = 13
Caption = 'Archivo:'
end
object Label4: TLabel
Left = 256
Top = 16
Width = 89
Height = 13
Caption = 'Contenido archivo:'
end
object NombreEdit: TEdit
Left = 80
Top = 80
Width = 121
Height = 21
TabOrder = 0
end
object DireccionEdit: TEdit
Left = 80
Top = 112
Width = 121
Height = 21
TabOrder = 1
end
object Button1: TButton
Left = 80
Top = 152
Width = 75
Height = 25
Caption = 'Salvar'
TabOrder = 2
OnClick = Button1Click
end
object ArchivoEdit: TEdit
Left = 80
Top = 16
Width = 121
Height = 21
TabOrder = 3
Text = 'datos.dat'
end
object Memo1: TMemo
Left = 256
Top = 32
Width = 241
Height = 129
TabOrder = 4
end
object Button2: TButton
Left = 320
Top = 168
Width = 75
Height = 25
Caption = 'Cargar'
TabOrder = 5
OnClick = Button2Click
end
end
//------------------------------------------------------------------------
//UNIT1.DFM HASTA AQUI (NO COPIES ESTO)
//------------------------------------------------------------------------
3.- Vuelve la forma Unit1 a su forma normal. Dando click derecho sobre el código y seleccionado en el menu la opción "View as Form".
4.- Copia el siguiente código en Unit1.cpp
//------------------------------------------------------------------------
//UNIT1.CPP DESDE AQUI
//------------------------------------------------------------------------
//Esta estructura es la que salvaremos en el archivo
typedef struct _Datos{
char nombre[50];
char direccion[100];
}TDatos;
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
//En caso que no hayas definido un archivo, abortamos
if(ArchivoEdit->Text.IsEmpty())
return;
//a continuacion copiamos los datos escritos a la estructura
TDatos DatosAGuardar;
strncpy(DatosAGuardar.nombre, NombreEdit->Text.c_str(), 49);
DatosAGuardar.nombre[49] = 0;
strncpy(DatosAGuardar.direccion, DireccionEdit->Text.c_str(), 99);
DatosAGuardar.direccion[99] = 0;
TFileStream* File;
//primero verificamos si existe el archivo
if(!FileExists(ArchivoEdit->Text)) //si no existe lo creamos
File = new TFileStream(ArchivoEdit->Text, fmCreate);
else //si existe lo abrimos
File = new TFileStream(ArchivoEdit->Text, fmOpenReadWrite);
//nos movemos al final del archivo
File->Seek(0, soFromEnd);
//escribimos el contenido de la estructura
File->Write(&DatosAGuardar, sizeof(TDatos));
delete File;
}
//---------------------------------------------------------------------------
void __fastcall TFo

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas