Problem with application

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
giuseppe.sportelli
In need of some credit
In need of some credit
Posts: 8
Joined: Sat May 09, 2020 12:47 pm

Problem with application

Post by giuseppe.sportelli »

I've resolved the problem with my application with two frame.
I've a strange problem.
First frame require a user input of data.
Data are stored in a binary file.
When i call second frame data are showed in grad in correct mode.
Now if i want to activate second frame from first to see data, program ends.
I think that the code don't open file in correct mode.
Here the code

Code: Select all

#include <fstream>
#include "Finestra_Visualizza.h"
#include "App_ArchivioApp.h"
using namespace std;
fstream file1;
struct alunno
{
    string cognome;
    string nome;
    string classe;
    string sezione;
    string data;
};
struct alunno a[1000];
int k;
//(*InternalHeaders(Finestra_Visualizza)
#include <wx/string.h>
#include <wx/intl.h>
//*)
//(*IdInit(Finestra_Visualizza)
const long Finestra_Visualizza::ID_GRID1 = wxNewId();
const long Finestra_Visualizza::ID_BUTTON1 = wxNewId();
//*)

BEGIN_EVENT_TABLE(Finestra_Visualizza,wxFrame)
	//(*EventTable(Finestra_Visualizza)
	//*)
END_EVENT_TABLE()

Finestra_Visualizza::Finestra_Visualizza(wxWindow* parent,wxWindowID id,const wxPoint& pos,const wxSize& size)
{
	//(*Initialize(Finestra_Visualizza)
	wxBoxSizer* BoxSizer1;

	Create(parent, wxID_ANY, _("Dati degli alunni"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE, _T("wxID_ANY"));
	BoxSizer1 = new wxBoxSizer(wxHORIZONTAL);
	grdati = new wxGrid(this, ID_GRID1, wxDefaultPosition, wxSize(640,239), 0, _T("ID_GRID1"));
	grdati->CreateGrid(0,5);
	grdati->EnableEditing(true);
	grdati->EnableGridLines(true);
	grdati->SetRowLabelSize(25);
	grdati->SetColLabelValue(0, _("Nome"));
	grdati->SetColLabelValue(1, _("Cognome"));
	grdati->SetColLabelValue(2, _("Classe"));
	grdati->SetColLabelValue(3, _("Sezione"));
	grdati->SetColLabelValue(4, _("Data Nascita"));
	grdati->SetDefaultCellFont( grdati->GetFont() );
	grdati->SetDefaultCellTextColour( grdati->GetForegroundColour() );
	BoxSizer1->Add(grdati, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	cmdchiudi = new wxButton(this, ID_BUTTON1, _("Chiudi Finestra"), wxDefaultPosition, wxSize(392,32), 0, wxDefaultValidator, _T("ID_BUTTON1"));
	BoxSizer1->Add(cmdchiudi, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	SetSizer(BoxSizer1);
	BoxSizer1->Fit(this);
	BoxSizer1->SetSizeHints(this);

	Connect(ID_GRID1,wxEVT_GRID_CELL_LEFT_CLICK,(wxObjectEventFunction)&Finestra_Visualizza::OngrdatiCellLeftClick);
	Connect(ID_BUTTON1,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&Finestra_Visualizza::OncmdchiudiClick);
	Connect(wxID_ANY,wxEVT_CLOSE_WINDOW,(wxObjectEventFunction)&Finestra_Visualizza::OnClose);
	//*)
	file1.open("archivio.dat",ios::in|ios::binary);
	k=0;
	while (!file1.eof())
    {
    file1.read((char*)&a[k],sizeof(struct alunno));
    k++;
    }
     file1.close();
	grdati->AutoSize();
	wxString s;
	int i=0;
	int j=0;
	//grdati->CreateGrid(10,5);
	grdati->EnableEditing(true);
	grdati->EnableGridLines(true);
	grdati->AppendRows(k);
	for (i=0;i<k;i++)
    {
        grdati->SetCellValue(i,j,wxString(a[i].cognome));
    }
    j++;
    for (i=0;i<k;i++)
    {
        grdati->SetCellValue(i,j,wxString(a[i].nome));
    }
    j++;
    for (i=0;i<k;i++)
    {
        grdati->SetCellValue(i,j,wxString(a[i].classe));
    }
    j++;
    for (i=0;i<k;i++)
    {
        grdati->SetCellValue(i,j,wxString(a[i].sezione));
    }
    j++;

    for (i=0;i<k;i++)
    {
        grdati->SetCellValue(i,j,wxString(a[i].data));
    }

}

Finestra_Visualizza::~Finestra_Visualizza()
{
	//(*Destroy(Finestra_Visualizza)
	//*)
}



void Finestra_Visualizza::OnClose(wxCloseEvent& event)
{
    //App_ArchivioFrame * tempfrm=new App_ArchivioFrame(NULL);
    //tempfrm->Show();
    Close();
}


void Finestra_Visualizza::OncmdchiudiClick(wxCommandEvent& event)
{
     Close();
      //App_ArchivioFrame->Show(True);
}

void Finestra_Visualizza::OngrdatiCellLeftClick(wxGridEvent& event)
{
}
Which are the problem ?
giuseppe.sportelli
In need of some credit
In need of some credit
Posts: 8
Joined: Sat May 09, 2020 12:47 pm

Re: Problem with application

Post by giuseppe.sportelli »

Here the part of code that write data on the files

Code: Select all

void App_ArchivioFrame::OncmdinsClick(wxCommandEvent& event)
{
    wxString temp;
    temp=txtn->GetValue();
    alunno[z].nome=string(temp.mb_str());
    temp="";
    temp=txtcg->GetValue();
    alunno[z].cognome=string(temp.mb_str());
    temp="";
    temp=txtcl->GetValue();
    alunno[z].classe=string(temp.mb_str());
    temp="";
    temp=txts->GetValue();
    alunno[z].sezione=string(temp.mb_str());
    temp="";
    temp=txtdn->GetValue();
    alunno[z].data=string(temp.mb_str());
    z++;
    azzera();
}
void App_ArchivioFrame::OncmdfineClick(wxCommandEvent& event)
{   int i=0;
    for (i=0;i<z;i++)
        file.write((char*)&alunno[i],sizeof(struct a));
    file.close();
}
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Problem with application

Post by doublemax »

First of all, you should use a debug build of your application, so when it crashes, it doesn't just terminate. Instead you can see where it crashes which should give you an idea about what the problem is.

Code: Select all

file.write((char*)&alunno[i],sizeof(struct a));
That doesn't work this way, the same goes for the equivalent loading code.

This will only save the internal data of std::string, but this will not include the actual characters. E.g. sizeof(struct a) will always be the same, no matter how long the strings are. Maybe you should look into wxFileConfig or an external JSON library to save and load text data.

Besides, it almost looks as if you're using this file to share the data with other parts of your application. If that's true, it would of course be a horrible way to do this.
Use the source, Luke!
giuseppe.sportelli
In need of some credit
In need of some credit
Posts: 8
Joined: Sat May 09, 2020 12:47 pm

Re: Problem with application

Post by giuseppe.sportelli »

Hello thank for attention
I always use file in c++ to store dato in binary mode through struct.
I Neverl have had problem with thai approach.
I wanted try this approach in gui programming
I know that json it is good solution.
Consider that my data struct can be' change in the type for exanple float string int in same struct.
I write and read struct data in file in this mode.
I presume that i write data the conversione between wxstring of the controls to string and viceversa are the problem.
For debug i've actived this modality.
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7479
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Problem with application

Post by ONEEYEMAN »

Hi,
Why do you need to save the data to the file? You do know that hard drive communication is slower than in-memory one?

You can just pass the data you need either in the constructor or wit the setters...

Thank you.
giuseppe.sportelli
In need of some credit
In need of some credit
Posts: 8
Joined: Sat May 09, 2020 12:47 pm

Re: Problem with application

Post by giuseppe.sportelli »

Hi, i need to save the data in file in C++ style mode, because the data will be loaded from old
console application write in C++.
I may create a class for date that is a good idea.
But i'd like understand first the problem.
I convert data string when i saving data.
I convert data entry box Text Control of the GUI in standard string.
When i loading data in wxGrid i loading it in standard string, and i need to convert string in WxString.
I think that is the problem.
If you want i can attach entire code block project in zip format.
It is a version to understand me, how to function the worderful WxWidgets API with C++.
Goodbye
giuseppe.sportelli
In need of some credit
In need of some credit
Posts: 8
Joined: Sat May 09, 2020 12:47 pm

Re: Problem with application

Post by giuseppe.sportelli »

he, you write

>> You can just pass the data you need either in the constructor or wit the setters...
I'm using event driven code in costructor i load startup settings, nothing of the I/O operation.
giuseppe.sportelli
In need of some credit
In need of some credit
Posts: 8
Joined: Sat May 09, 2020 12:47 pm

Re: Problem with application

Post by giuseppe.sportelli »

I've resolved the problem was in files function representation !
i've a other small question:
When i call second frame through event button in the code i write

Code: Select all

void App_ArchivioFrame::OncmdvisClick(wxCommandEvent& event)
{
    Iconize();
    Finestra_Visualizza * childFrame = new Finestra_Visualizza(this);
    childFrame->Show();

}
.
It'is ok, but when i wnat to call close button in second frame i want to maximized first frame.
How can i do this ?
I've noted that i lose object reference.
Is it corret ?

Thank for support.
Note that i record video on youtube in italian to explain this ! In italian Language
https://www.youtube.com/playlist?list=P ... tjbSW66Qd4
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Problem with application

Post by doublemax »

If the user should not be able to use the first frame when the second is opened, maybe a modal dialog would be the better option.

Then the program flow would look like this.

Code: Select all

void App_ArchivioFrame::OncmdvisClick(wxCommandEvent& event)
{
   Iconize();

   Finestra_Visualizza dlg(this);          // Finestra_Visualizza must derive from wxDialog instead of wxFrame
   dlg.ShowModal();                       // code execution stops here until the dialog is closed

   Iconize(false);
}
Use the source, Luke!
Post Reply