Home > Software engineering >  How to use ifstream and ofstream in MFC list box control implementation file read and write?
How to use ifstream and ofstream in MFC list box control implementation file read and write?

Time:09-23

What I use is vs2017, do topic, the content of the class the teacher requests the book requires through ifstream and ofstream list of MFC implementation file input and output, and other requirements, file compiling succeeded but there is no way to create a TXT file and read and write the file, the code are made according to the book, a little long, bosses, please help to find out what is going on, thank you!
 # include "stdafx. H" 
# include "List6005. H"
# include "List6005Dlg. H"
# include "afxdialogex. H"
# include & lt; Fstream>//the ifstream and ofstream header file, at the time of use must be combined with namespace
# include & lt; Vector>
# include & lt; String>
using namespace std;
# ifdef _DEBUG
# define new DEBUG_NEW
# endif

Using STD: : vector;
Using STD: : string;

//for application "about" menu item CAboutDlg dialog

The class CAboutDlg: public CDialogEx
{
Public:
CAboutDlg ();

//data dialog
# ifdef AFX_DESIGN_TIME
Enum {IDD=IDD_ABOUTBOX};
# endif

Protected:
Virtual void DoDataExchange (CDataExchange * symbol;//support DDX/DDV

//implementation
Protected:
DECLARE_MESSAGE_MAP ()
};

CAboutDlg: : CAboutDlg () : CDialogEx (IDD_ABOUTBOX)
{
}

Void CAboutDlg: : DoDataExchange (CDataExchange * symbol)
{
CDialogEx: : DoDataExchange (symbol);
}

BEGIN_MESSAGE_MAP (CAboutDlg CDialogEx)
END_MESSAGE_MAP ()


//CList6005Dlg dialog



CList6005Dlg: : CList6005Dlg (CWnd *=nullptr pParent/* */)
: CDialogEx (IDD_LIST6005_DIALOG, pParent)
{
M_hIcon=AfxGetApp () - & gt; LoadIcon (IDR_MAINFRAME);
}

Void CList6005Dlg: : DoDataExchange (CDataExchange * symbol)
{
CDialogEx: : DoDataExchange (symbol);
DDX_Control (symbol, IDC_LIST1 m_List);//associated member variable
DDX_Control (symbol, IDC_STATIC1 m_CityName);
DDX_Control (symbol, IDC_STATIC2 m_CityInfo);
}

BEGIN_MESSAGE_MAP (CList6005Dlg, CDialogEx)//message map

ON_WM_SYSCOMMAND ()
ON_WM_PAINT ()
ON_WM_QUERYDRAGICON ()
ON_LBN_SELCHANGE (IDC_LIST1, & amp; CList6005Dlg: : OnSelchangeList16005)
ON_BN_CLICKED (IDC_BUTTON2, & amp; CList6005Dlg: : OnClickedButton60052)

ON_BN_CLICKED (IDC_BUTTON1, & amp; CList6005Dlg: : OnClickedBuDelete6005)
ON_BN_CLICKED (IDC_BUTTON3, & amp; CList6005Dlg: : OnClickedBuReset)
ON_COMMAND (IDD_LIST6005_DIALOG, & amp; CList6005Dlg: : OnClose)
END_MESSAGE_MAP ()


//CList6005Dlg message handler

BOOL CList6005Dlg: : OnInitDialog ()//initializes the
{
CDialogEx: : OnInitDialog ();

//will "about... "Menu item added to the system menu,
Ifstream (in G: \ \ "TXT \ \ city. TXT");//create a file input stream
String city_name, city_info;
While (in & gt;> City_name & gt;> City_info)//read the city name and the information in the file
{
City. The push_back (city_name);//add the name of the city to city, as the next row
City. The push_back (city_info);
M_List. AddString (city_name c_str ());//add the city name to the list box
}
in.close();//close the file flow


//IDM_ABOUTBOX must be in the system command range,
ASSERT ((IDM_ABOUTBOX & amp; 0==IDM_ABOUTBOX xfff0));
ASSERT (IDM_ABOUTBOX & lt; 0 xf000);

CMenu * pSysMenu=GetSystemMenu (FALSE);
If (pSysMenu!=nullptr)
{
BOOL bNameValid;
Cstrings strAboutMenu;
BNameValid=strAboutMenu. LoadString (IDS_ABOUTBOX);
ASSERT (bNameValid);
if (! StrAboutMenu. IsEmpty ())
{
PSysMenu - & gt; The AppendMenu (MF_SEPARATOR);
PSysMenu - & gt; AppendMenu (MF_STRING IDM_ABOUTBOX, strAboutMenu);
}
}

//set this dialog ICONS, when the application is the main window is not a dialog, the framework will automatically
//do this
SetIcon (m_hIcon, TRUE);//set the large icon
SetIcon (m_hIcon, FALSE);//set the small icon

//TODO: add additional initialization code in the

return TRUE;//unless set focus to the control, return TRUE
}

Void CList6005Dlg: : OnSysCommand (UINT nID, LPARAM LPARAM)
{
If ((nID & amp; 0 xfff0)==IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
DlgAbout. DoModal ();
}
The else
{
CDialogEx: : OnSysCommand (nID, lParam);
}
}

//if the dialog box to add a minimize button, you need the following code
//to draw the icon, to use the document/view model of MFC application,
//it will be done automatically by the framework,

Void CList6005Dlg: : OnPaint ()
{
If (IsIconic ())
{
CPaintDC dc (this);//for drawing device context

The SendMessage (WM_ICONERASEBKGND, reinterpret_cast & lt; WPARAM> (dc) GetSafeHdc ()), 0);

//make the icon is centered within the workspace rectangular
Int cxIcon=GetSystemMetrics (SM_CXICON);
Int cyIcon=GetSystemMetrics (SM_CYICON);
CRect the rect.
GetClientRect (& amp; The rect);
Int x=(the rect. Width () - cxIcon + 1)/2;
Int y=(the rect. Height () - cyIcon + 1)/2;

//draw the icon
Dc. DrawIcon (x, y, m_hIcon);
}
The else
{
CDialogEx: : OnPaint ();
}
}

//when the user drag minimize window system call this function to obtain the cursor
//show,
HCURSOR CList6005Dlg: : OnQueryDragIcon ()
{
Return static_cast & lt; HCURSOR> (m_hIcon);
}



Void CList6005Dlg: : OnSelchangeList16005 ()
{
//TODO: add the control notification handler code
Vector . : : size_type t=m_List GetCurSel ();//get the current item subscript
M_CityName. SetWindowText (city [t] c_str ());//set shows the city name
M_CityInfo. SetWindowText (info [t] c_str ());//set the display city information
}


Void CList6005Dlg: : OnClickedButton60052 ()
{
//TODO: add the control notification handler code
Cstrings city_name, city_info;
GetDlgItem (IDC_EDIT1) - & gt; GetWindowText (city_name); nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related