He estado compicado con muchas cosas y no habia tenido la oportunidad de entrar a los foros y demas...
El tema que me trae es en el mismo que me has ayudado mucho sobr eficheros binarios y como leer, escribir, editar y eliminar de estos aca te dejo el codigo que una vez, amablemente me distes.
//--------------------------------------------------------------------------- #include <vcl.h> #pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" #include <fstream.h> #include <fcntl.h> #include<io.h> using namespace std; struct tipoRegistro { char nombre[32]; int edad; float altura; }; class TRegistros { private: TList* List; public: TRegistros(); ~TRegistros(); tipoRegistro& operator [] (int); int GetCount(); void Insert(tipoRegistro& R); void Add(tipoRegistro& R); void Delete(int); void ReadFile(char* FileName); void WriteFile(char* FileName); void Clear(); }; TRegistros::TRegistros() { List = new TList(); } TRegistros::~TRegistros() { Clear(); delete List; } // libera todos los registros void TRegistros::Clear() { for(int n=0; n<List->Count;n++){ delete List->Items[n]; List->Items[n] = 0; } List->Clear(); } int TRegistros::GetCount() { return List->Count; } // Devuelve una referencia a un tipoRegistro pedido. // Si está fuera de rango devuelve el correspondiente al modulo. tipoRegistro& TRegistros::operator [] (int i) { return *(tipoRegistro*)List->Items[i&List->Count]; } void TRegistros::Add(tipoRegistro& R) { tipoRegistro* NewR = new tipoRegistro; *NewR = R; List->Add(NewR); } void TRegistros::Delete(int i) { delete List->Items[i]; List->Delete(i); } // Esto lo puedes hacer con fstream también void TRegistros::ReadFile(char* FileName) { int File = open(FileName, O_BINARY); if(File ==-1) return; int Size = filelength(File); int Count = Size/sizeof(tipoRegistro); tipoRegistro* Items = new tipoRegistro[Count]; read(File, (void*)Items, Size); close(File); Clear(); for(int n=0; n<Count; n++) Add(Items[n]); delete [] Items; } /* // Esto lo puedes hacer con el sistema de bajo nivel también. // Funciona bien void TRegistros::WriteFile(char* FileName) { ofstream fsalida(FileName, ios::trunc | ios::binary); for(int n=0; n<List->Count; n++) fsalida.write((char*)(List->Items[n]),sizeof(tipoRegistro)); fsalida.close(); } */ // Esto con el sistema de bajo nivel void TRegistros::WriteFile(char* FileName) { int File = open(FileName, O_CREAT | O_TRUNC | O_BINARY | O_RDWR); if(File ==-1) return; for(int n=0; n<List->Count; n++) write(File, List->Items[n], sizeof(tipoRegistro)); close(File); } TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { // Creo un objeto de la clase TRegistros: TRegistros Registros; // Creo un registro: tipoRegistro Reg; strcpy(Reg.nombre, "Jose Luis"); Reg.edad = 32; Reg.altura = 1.78; // Añado el registro: Registros.Add(Reg); strcpy(Reg.nombre, "Pepe Juan"); Reg.edad = 35; Reg.altura = 2.05; // Añado el registro: Registros.Add(Reg); strcpy(Reg.nombre, "Maria"); Reg.edad = 25; Reg.altura = 1.70; // Añado el registro: Registros.Add(Reg); // Escribo archivo Registros.WriteFile("Prueba.dat"); } //--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender) { // Creo un objeto de la clase TRegistros: TRegistros Registros; // Leo archivo Registros.ReadFile("Prueba.dat"); for(int i=0; i<Registros.GetCount(); i++){ Memo1->Lines->Add(Registros[i].nombre); Memo1->Lines->Add("Edad: "+IntToStr(Registros[i].edad)); Memo1->Lines->Add("Talla: "+CurrToStr(Registros[i].altura)); } } //---------------------------------------------------------------------------
Como ves en la construccion del formulario ya tu agregar todos lo valores, cosa que haria yo desde unos edits...Tampoco truncaria el fichero cuando lo guardo, sino que agregaria la informacion nueva al final (int File = open(FileName, O_CREAT | O_APPEND | O_BINARY | O_RDWR).
Pero te comento que el problema me lo esta dando cuando leeo la informacion porque si guardo por ejemplo:
nombre: michel
edad: 34
alt: 1,78
y luego cualquier otro nombre con distintos datos
siempre me sale que hay dos registros , pero los dos son michel...
Podrias hacer una prueba y hecharle una ojeada por favor
muchs gracias