用vc做了个简单的例子
打开后,点击Start
出现单词后,点击Known或者Unknown
退出出需要点击Save进行保存
下次再打开的时候,会从上次保存的地方继续执行
known.txt中保存认识的单词
Unknown.txt保存不认识的单词
result.txt是单词数据
pos.txt 中第一行是已检测的数量,第二行是认识的数量
程序工程放到了我的资源中 http://download.csdn.net/detail/aaajj/5438173
/
// CAboutDlg dialog used for App About
std::fstream m_dicfile;
std::fstream ufile;
std::fstream nfile;
int m_knowNum;
int m_pos;
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/
// CWORDDlg dialog
CWORDDlg::CWORDDlg(CWnd* pParent /*=NULL*/)
: CDialog(CWORDDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CWORDDlg)
m_save = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CWORDDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CWORDDlg)
DDX_Text(pDX, IDC_EDIT1, m_save);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CWORDDlg, CDialog)
//{{AFX_MSG_MAP(CWORDDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnSave)
//}}AFX_MSG_MAP
ON_EN_CHANGE(IDC_EDIT2, &CWORDDlg::OnEnChangeEdit2)
ON_BN_CLICKED(IDC_BUTTON2, &CWORDDlg::OnBnClickedButton2)
ON_BN_CLICKED(IDC_BUTTON3, &CWORDDlg::OnBnClickedButton3)
ON_BN_CLICKED(IDC_BUTTON4, &CWORDDlg::OnBnClickedButton4)
END_MESSAGE_MAP()
/
// CWORDDlg message handlers
BOOL CWORDDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
void CWORDDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CWORDDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CWORDDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
//know
void CWORDDlg::OnSave()
{
// TODO: Add your control notification handler code here
m_knowNum++;
m_pos++;
CString astr;
GetDlgItemText(IDC_EDIT1,astr);
ufile << astr.GetBuffer() << std::endl;
char buf[1024] = {0};
m_dicfile.getline(buf, 1024);
m_save = buf;
SetDlgItemText(IDC_EDIT1,m_save);
sprintf(buf, "认识[%d], 已检测[%d]", m_knowNum, m_pos);
CString str2 = buf;
SetDlgItemText(IDC_EDIT2, str2);
}
void CWORDDlg::OnEnChangeEdit2()
{
// TODO: 如果该控件是 RICHEDIT 控件,则它将不会
// 发送该通知,除非重写 CDialog::OnInitDialog()
// 函数并调用 CRichEditCtrl().SetEventMask(),
// 同时将 ENM_CHANGE 标志“或”运算到掩码中。
// TODO: 在此添加控件通知处理程序代码
}
//unknown
void CWORDDlg::OnBnClickedButton2()
{
// TODO: 在此添加控件通知处理程序代码
m_pos++;
CString astr;
GetDlgItemText(IDC_EDIT1, astr);
nfile << astr.GetBuffer() << std::endl;
char buf[1024] = {0};
m_dicfile.getline(buf, 1024);
m_save = buf;
SetDlgItemText(IDC_EDIT1,m_save);
sprintf(buf, "认识[%d], 已检测[%d]", m_knowNum, m_pos);
CString str2 = buf;
SetDlgItemText(IDC_EDIT2, str2);
}
//save
void CWORDDlg::OnBnClickedButton3()
{
// TODO: 在此添加控件通知处理程序代码
m_dicfile.close();
ufile.close();
nfile.close();
std::fstream infile;
char buf[100];
if (m_pos == 0)
{
return;
}
_unlink("pos.txt");
infile.open("pos.txt", std::ios::app);
sprintf(buf, "%d", m_pos);
infile << buf << std::endl;
sprintf(buf, "%d", m_knowNum);
infile << buf << std::endl;
infile.close();
}
//start
void CWORDDlg::OnBnClickedButton4()
{
m_knowNum = 0;
// TODO: 在此添加控件通知处理程序代码
std::fstream infile("pos.txt");
char buf[1024] = {0};
int line = 0;
while(!infile.eof()) //判断文件是否读结束
{
infile.getline(buf, 1024);
if (line == 0)
{
m_pos = atoi(buf);
}
else
{
m_knowNum = atoi(buf);
break;
}
line++;
}
sprintf(buf, "认识[%d], 已检测[%d]", m_knowNum, m_pos);
CString str2 = buf;
SetDlgItemText(IDC_EDIT2, str2);
infile.close();
ufile.open("known.txt", std::ios::app);
nfile.open("Unknown.txt", std::ios::app);
m_dicfile.open("result.txt", std::ios::in);
// m_dicfile.open("result.txt", std::ios::app);
int i = 0;
while (!m_dicfile.eof())
{
m_dicfile.getline(buf, 1024);
if (i < m_pos)
{
i++;
continue;
}
else
{
m_save = buf;
SetDlgItemText(IDC_EDIT1, m_save);
break;
}
}
}