To do shapes in the form I use a vector that stores a struct with the x, y, color and shape values of every shape I put on the form.

I have to save and open these values.

To save and open files with shapes.


I use a TOpenDialog and a TSaveDialog.


I save the values fine, but when I have to write the open code I try that the same vector receive the saved values but it doesn't accept it.
It sends an exception with the vector.


I had to use 4 vectors, one for the x, other for the y, other for the color and other for the shape.


Could you tell me how can make it with one vector only?

#include <vcl.h>
#pragma hdrstop
#include "coordenadas.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{

}
//---------------------------------------------------------------------------
void __fastcall TForm1::BGuardarClick(TObject *Sender)
{
      AnsiString archivo;
                {
                 
       if(DGuardar->Execute()){
          archivo=DGuardar->FileName;
          ofstream outfile(archivo.c_str(),ios::binary);  //abrir archivo para escritura

          tam=vdata.size();
          outfile.write((char*)&tam,sizeof(tam));

        TShape *fig;
        fig = new TShape(this);
        fig->Parent =Form1;
        fig->Left=X;
        fig->Top=Y;
        fig->Height=20;
        fig->Width=20;     
           for(int k=0;k<vdata.size();k++){
           elemento=vdata[k].X;
           outfile.write((char*)&(elemento), sizeof(elemento));
           }
           for(int k=0;k<vdata.size();k++){
           elemento1=vdata[k].Y;
           outfile.write((char*)&(elemento1), sizeof(elemento1));
           }
           for(int k=0;k<vdata.size();k++){
           elemento2=vdata[k].color;
           outfile.write((char*)&(elemento2),sizeof(elemento2));
           }
           for(int k=0;k<vdata.size();k++){
           elemento3=vdata[k].forma;
           outfile.write((char*)&(elemento3),sizeof(elemento3));
           }

          }
          }     
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BAbrirClick(TObject *Sender)
{
        AnsiString archivo;
         
         if(DAbrir->Execute()){
            archivo=DAbrir->FileName;
            ifstream infile(archivo.c_str(),ios::binary);  //abrir archivo para lectura
           num.erase(num.begin(),num.end());
           num2.erase(num2.begin(),num2.end());
           num3.erase(num3.begin(),num3.end());
           num4.erase(num4.begin(),num4.end());
           
           vdata.erase(vdata.begin(),vdata.end());
           vf.erase(vf.begin(),vf.end());
            infile.read((char*)&tam,sizeof(int));
           
           {
            for(int k=0;k<tam;k++){
            infile.read((char*)&elemento,sizeof(elemento));
            num.insert(num.end(),elemento);
            
           
             }
            for(int k=0;k<tam;k++){
            infile.read((char*)&elemento1,sizeof(elemento1));
            num2.insert(num2.end(),elemento1);

            }
            for(int k=0;k<tam;k++){
            infile.read((char*)&elemento2,sizeof(elemento2));
            num3.insert(num3.end(),elemento2);
                        }
            for(int k=0;k<tam;k++){
            infile.read((char*)&elemento3,sizeof(elemento3));
            num4.insert(num4.end(),elemento3);
           
            }
            
               }
       
        for(k=0;k<tam;k++){
        TShape *fig;
        fig = new TShape(this);
        fig->Parent=Form1;
        fig->Left=num[k];
        fig->Top=num2[k];
        fig->Height=20;
        fig->Width=20;
        fig->Brush->Color=num3[k];
        fig->Shape=num4[k];
 
        vf.insert(vf.end(),fig);
 }
}
}
 
 
void __fastcall TForm1::FormMouseDown(TObject *Sender, TMouseButton Button,
      TShiftState Shift, int X, int Y)
{
        TShape *fig;
        fig = new TShape(this);
        fig->Parent =Form1;
        fig->Left=X;
        fig->Top=Y;
        fig->Height=20;
        fig->Width=20;

        
        if(TBCirculo->Down){
        fig->Shape=stCircle;
        fig->Brush->Color=clLime;
        }
        else{
        fig->Shape=stRectangle;
        fig->Brush->Color=clLime;
        }
        if(TBYellow->Down){
        fig->Brush->Color=clYellow;
        }
        else if(TBOlive->Down){
        fig->Brush->Color=clOlive;
        }
        else if (TBNavy->Down){
        fig->Brush->Color=clNavy;
        }
        else if(TBTeal->Down){
        fig->Brush->Color=clTeal;
        }
        else if(TBAqua->Down){
        fig->Brush->Color=clAqua;
        }
        else if(TBPurple->Down){
        fig->Brush->Color=clPurple;
        }
        else if(TBRed->Down){
        fig->Brush->Color=clRed;
        }
        figu.X=fig->Left;
        figu.Y=fig->Top;
        figu.color=fig->Brush->Color;
        figu.forma=fig->Shape;
        Lx->Items->Add(figu.X);
        Ly->Items->Add(figu.Y);
        Lcolor->Items->Add(figu.color);
        Lforma->Items->Add(figu.forma);
        
        vdata.insert(vdata.end(),figu);
               
        vf.insert(vf.end(),fig);
        
}

you can create a structure that contains the x, y, color and shape, then have a vector of those structs.

you can create a structure that contains the x, y, color and shape, then have a vector of those structs.

I already did that struct and the vector and I save this in a file, but when I have to open the file, in the code to open it, I want to use the same vector to store the values saved in the file, but it sends me an exception.

post your code -- I lost my binoculars the other day so I can't see your computer screen. :)

post your code -- I lost my binoculars the other day so I can't see your computer screen. :)

Well, I had already posted the code in my first post. But here is again.

void __fastcall TForm1::FormMouseDown(TObject *Sender, TMouseButton Button,
      TShiftState Shift, int X, int Y)
{
        TShape *fig;
        fig = new TShape(this);
        fig->Parent =Form1;
        fig->Left=X;
        fig->Top=Y;
        fig->Height=20;
        fig->Width=20;

        
        if(TBCirculo->Down){
        fig->Shape=stCircle;
        fig->Brush->Color=clLime;
        }
        else{
        fig->Shape=stRectangle;
        fig->Brush->Color=clLime;
        }
        if(TBYellow->Down){
        fig->Brush->Color=clYellow;
        }
        else if(TBOlive->Down){
        fig->Brush->Color=clOlive;
        }
        else if (TBNavy->Down){
        fig->Brush->Color=clNavy;
        }
        else if(TBTeal->Down){
        fig->Brush->Color=clTeal;
        }
        else if(TBAqua->Down){
        fig->Brush->Color=clAqua;
        }
        else if(TBPurple->Down){
        fig->Brush->Color=clPurple;
        }
        else if(TBRed->Down){
        fig->Brush->Color=clRed;
        }
        figu.X=fig->Left;
        figu.Y=fig->Top;
        figu.color=fig->Brush->Color;
        figu.forma=fig->Shape;
               
        vdata.insert(vdata.end(),figu);
                
        vf.insert(vf.end(),fig);
        
}
 
 
 
 
void __fastcall TForm1::BGuardarClick(TObject *Sender)
{
      AnsiString archivo;
                {
                 
       if(DGuardar->Execute()){
          archivo=DGuardar->FileName;
          ofstream outfile(archivo.c_str(),ios::binary);  //abrir archivo para escritura

          tam=vdata.size();
          outfile.write((char*)&tam,sizeof(tam));

        TShape *fig;
        fig = new TShape(this);
        fig->Parent =Form1;
        fig->Left=X;
        fig->Top=Y;
        fig->Height=20;
        fig->Width=20; 
 
    
           for(int k=0;k<vdata.size();k++){
           elemento=vdata[k].X;
           outfile.write((char*)&(elemento), sizeof(elemento));
           }
           for(int k=0;k<vdata.size();k++){
           elemento1=vdata[k].Y;
           outfile.write((char*)&(elemento1), sizeof(elemento1));
           }
           for(int k=0;k<vdata.size();k++){
           elemento2=vdata[k].color;
           outfile.write((char*)&(elemento2),sizeof(elemento2));
           }
           for(int k=0;k<vdata.size();k++){
           elemento3=vdata[k].forma;
           outfile.write((char*)&(elemento3),sizeof(elemento3));
           }

          }
          }     
}

:?: Here begins my problem:

void __fastcall TForm1::BAbrirClick(TObject *Sender)
{
        AnsiString archivo;
         
         if(DAbrir->Execute()){
            archivo=DAbrir->FileName;
            ifstream infile(archivo.c_str(),ios::binary);  //abrir archivo para lectura
 
 
 :?: I USE 4 VECTORS, BUT I WANT TO USE JUST ONE.
 
 
 
           num.erase(num.begin(),num.end());
           num2.erase(num2.begin(),num2.end());
           num3.erase(num3.begin(),num3.end());
           num4.erase(num4.begin(),num4.end());
           
           vdata.erase(vdata.begin(),vdata.end());
           vf.erase(vf.begin(),vf.end());
            infile.read((char*)&tam,sizeof(int));
           
           {
            for(int k=0;k<tam;k++){
            infile.read((char*)&elemento,sizeof(elemento));
            num.insert(num.end(),elemento);
            
                        }
            for(int k=0;k<tam;k++){
            infile.read((char*)&elemento1,sizeof(elemento1));
            num2.insert(num2.end(),elemento1);

           
            }
            for(int k=0;k<tam;k++){
            infile.read((char*)&elemento2,sizeof(elemento2));
            num3.insert(num3.end(),elemento2);
                       }
            for(int k=0;k<tam;k++){
            infile.read((char*)&elemento3,sizeof(elemento3));
            num4.insert(num4.end(),elemento3);
                      }
            
    //        vdata.insert(vdata.end(),figu);
            }
 
       
        for(k=0;k<tam;k++){
        TShape *fig;
        fig = new TShape(this);
        fig->Parent=Form1;
        fig->Left=num[k];
        fig->Top=num2[k];
        fig->Height=20;
        fig->Width=20;
        fig->Brush->Color=num3[k];
        fig->Shape=num4[k];
 
        vf.insert(vf.end(),fig);
 }
}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.