标题:求帮忙修改一下我的MFC,程序可以保存但读不出来!!!不知道是不是保存格式 ...
取消只看楼主
Candicechan
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2013-6-11
结帖率:0
已结贴  问题点数:10 回复次数:0 
求帮忙修改一下我的MFC,程序可以保存但读不出来!!!不知道是不是保存格式有问题
[local]1[/local]


程序在压缩包,求修改····感激不尽求修改····感激不尽


1.h


#pragma once
#include "afxwin.h"


// C1 对话框
struct V
{
    wchar_t name[4];
  int age;
  int sex;
  int y;
  int sf;
  int oral;

};

class C1 : public CPropertyPage
{
    DECLARE_DYNAMIC(C1)

public:
    C1();
    C1(CPropertySheet *p);
    virtual ~C1();
    virtual BOOL OnInitDialog();


// 对话框数据
    enum { IDD = IDD_PROPPAGE_LARGE1 };

protected:
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持

    DECLARE_MESSAGE_MAP()
public:
    afx_msg void OnCbnSelchangeCombo2();
public:
    CPropertySheet *m_p;
    CComboBox m_a1;
public:
    CComboBox m_a2;
public:
    CComboBox m_a3;
public:
    afx_msg void OnCbnSelchangeCombo1();
public:
    afx_msg void OnCbnSelchangeCombo3();
public:
    afx_msg void OnBnClickedSave();
public:
    int m_age;
public:
    int m_man;
public:
    int m_woman;
public:
    CString m_name;
};


// 1.cpp : 实现文件
//

#include "stdafx.h"
#include "AG.h"
#include "1.h"
#include "fstream"
using namespace std;

bool g_change = false;

// C1 对话框

IMPLEMENT_DYNAMIC(C1, CPropertyPage )

C1::C1(CPropertySheet *p)
    : CPropertyPage(C1::IDD)
    , m_age(0)
    , m_man(0)
    , m_woman(0)
    , m_name(_T(""))
{
  
}

C1::~C1()
{
}

void C1::DoDataExchange(CDataExchange* pDX)
{
    CPropertyPage::DoDataExchange(pDX);
    DDX_Control(pDX, IDC_COMBO1, m_a1);
    DDX_Control(pDX, IDC_COMBO2, m_a2);
    DDX_Control(pDX, IDC_COMBO3, m_a3);

    DDX_Text(pDX, IDC_NAME, m_name);
    DDX_Text(pDX, IDC_AGE, m_age);

    DDX_Check(pDX,IDC_MAN,m_man);
    DDX_Check(pDX,IDC_WOMAN,m_woman);

}


BEGIN_MESSAGE_MAP(C1, CPropertyPage)
    ON_CBN_SELCHANGE(IDC_COMBO2, &C1::OnCbnSelchangeCombo2)
    ON_CBN_SELCHANGE(IDC_COMBO1, &C1::OnCbnSelchangeCombo1)
    ON_CBN_SELCHANGE(IDC_COMBO3, &C1::OnCbnSelchangeCombo3)
    ON_BN_CLICKED(IDC_SAVE, &C1::OnBnClickedSave)
END_MESSAGE_MAP()


// C1 消息处理程序
BOOL C1::OnInitDialog()
{
    CPropertyPage::OnInitDialog();
    m_a1.AddString(_T("酒店管理"));
    m_a1.AddString(_T("应用数学"));
    m_a1.AddString(_T("英语"));
    m_a1.AddString(_T("金融学"));

    m_a2.AddString(_T("广东"));
    m_a2.AddString(_T("上海"));
    m_a2.AddString(_T("云南"));
    m_a2.AddString(_T("海南"));

    m_a3.AddString(_T("A"));
    m_a3.AddString(_T("B"));
    m_a3.AddString(_T("C"));
    m_a3.AddString(_T("D"));

    int nIndex = m_a1.GetCurSel();
    CString aBText;
    m_a1.GetLBText(nIndex, aBText);

    /*((CButton *)GetDlgItem(IDC_MAN))->SetCheck(1);
    CComboBox *pBox = ((CComboBox *)GetDlgItem(IDC_COMBO1));
    pBox->SetCurSel(2);
    //CComboBox *pBox = ((CComboBox *)GetDlgItem(IDC_COMBO2));
   
    //CComboBox *pBox = ((CComboBox *)GetDlgItem(IDC_COMBO3));
*/
    CEdit*pBoxOne;
    pBoxOne = (CEdit *)GetDlgItem(IDC_NAME);
    pBoxOne ->SetWindowTextW(_T("ffff"));
    CString str;
    pBoxOne ->GetWindowTextW(str);

    return true;

}

void C1::OnCbnSelchangeCombo1()
{
    UpdateData( true);
    int index=m_a1.GetCurSel();
    if(index==1)
        m_a1.AddString(_T("酒店管理"));
    if(index==2)
        m_a1.AddString(_T("应用数学"));
    if(index==3)
        m_a1.AddString(_T("英语"));
    if(index==4)
        m_a1.AddString(_T("金融学"));
    UpdateData( false);
        
}
void C1::OnCbnSelchangeCombo2()
{   
    UpdateData( true);
    int index=m_a1.GetCurSel();
     if(index==5)
        m_a2.AddString(_T("广东"));
    if(index==6)
        m_a2.AddString(_T("云南"));
    if(index==7)
        m_a2.AddString(_T("海南"));
    if(index==8)
        m_a2.AddString(_T("上海"));
    UpdateData( false);
}


void C1::OnCbnSelchangeCombo3()
{
    UpdateData( true);
    int index=m_a1.GetCurSel();
    if(index==9)
        m_a3.AddString(_T("A"));
    if(index==10)
        m_a3.AddString(_T("B"));
    if(index==11)
        m_a3.AddString(_T("C"));
    if(index==12)
        m_a3.AddString(_T("D"));
    UpdateData( false);
}

void C1::OnBnClickedSave()
{
    UpdateData(true);
    V v;
    v="abc";
    CFile infile;
    infile.Open("D:\\a.txt",CFile::modeWrite|CFile::modeCreate|CFile::modeNoTruncate);
    infile.SeekToEnd();
    infile.Write((LPCSTR)v,v.name()+4);
    infile.Close();

    /*memset(&v, 0 ,sizeof(V));

    v.age = m_age;
    v.sex = m_man;
   

    memcpy(v.name,m_name.GetBuffer(),m_name.GetLength()*2);//什么意思???

    int nIndex = m_a1.GetCurSel();
    CString str;
    m_a1.GetLBText( nIndex,str);
    //memcpy(v.y,str.GetBuffer(),str.GetLength()*2);

    ofstream inFile;
    inFile.open("D:\\a.txt", ios_base::out|ios_base::binary|ios_base::app);

    inFile.write((char *)&v,sizeof(V));

    inFile.close();

    m_name=_T("");
    m_age = 0;
    m_man = 0;
    m_woman = 1;
    UpdateData(false);*/
   
}


2.h
#pragma once
#include "afxwin.h"
#include "fstream"
using namespace std;


// C2 对话框

class C2 : public CPropertyPage
{
    DECLARE_DYNAMIC(C2)

public:
    C2();
    virtual ~C2();
    virtual BOOL OnInitDialog();
// 对话框数据
    enum { IDD = IDD_PROPPAGE_LARGE };

protected:
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持
    virtual BOOL OnSetActive();

    DECLARE_MESSAGE_MAP()

public:
    CComboBox m_b1;
public:
    CComboBox m_b2;
public:
    CComboBox m_b3;
public:
    CString m_name;
public:
    int m_age;
public:
    int m_man;
public:
    int m_woman;
public:
    ifstream inFile;
    int m_cnt;
    int m_index;

    afx_msg void OnEnChangeNameShow();
public:
    afx_msg void OnBnClickedP();
public:
    afx_msg void OnBnClickedN();
public:
    afx_msg void OnBnClickedR();
};



// 2.cpp : 实现文件
//

#include "stdafx.h"
#include "AG.h"
#include "2.h"
#include "1.h"

#include "fstream"
using namespace std;

extern bool g_change;


// C2 对话框

IMPLEMENT_DYNAMIC(C2, CPropertyPage)

C2::C2()
    : CPropertyPage(C2::IDD)
    , m_name(_T(""))
    , m_age(0)
    , m_man(0)
    , m_woman(0)
{
    inFile.open("D:\\a.txt", ios_base::in|ios_base::binary);
}

C2::~C2()
{
    inFile.close();
}

void C2::DoDataExchange(CDataExchange* pDX)
{
    CPropertyPage::DoDataExchange(pDX);
    DDX_Control(pDX, IDC_COMBO1, m_b1);
    DDX_Control(pDX, IDC_COMBO2, m_b2);
    DDX_Control(pDX, IDC_COMBO3, m_b3);

    DDX_Text(pDX, IDC_EDIT1, m_name);
    DDX_Text(pDX, IDC_EDIT3, m_age);

    DDX_Check(pDX,IDC_RADIO1,m_man);
    DDX_Check(pDX,IDC_RADIO2,m_woman);


}
BOOL C2::OnSetActive()
{
    CPropertyPage::OnSetActive();

    if(g_change== true)
    {
        inFile.close();
        inFile.open("D:\\a.txt", ios_base::in|ios_base::binary);
        
        inFile.seekg(0,ios_base::end);
        
        m_cnt = inFile.tellg()/sizeof(V);

        inFile.seekg(0,ios_base::beg);
        
        if(m_cnt == 0)
        {
            m_index = 0;
            return true;
        }
        if(m_cnt <m_index)
            m_index =m_cnt;
        if(m_index == 0)
            m_index = 1;
        inFile.seekg ((m_index -1)*sizeof(V),ios_base::beg);
         
        V v;
        memset(&v,0,sizeof(V));
        inFile.read((char *)&v, sizeof(V));

        m_name = v.name;
        m_age = v.age;
        m_man = v.sex;
        m_woman = 1-v.sex;

        UpdateData(false);

    }
    g_change = false;
    return true;

}


BOOL C2::OnInitDialog()
{
    CPropertyPage::OnInitDialog();

    /*inFile.seekg (0,ios_base::end);

    m_cnt = inFile.tellg()/sizeof(V);

    inFile.seekg (0,ios_base::beg);

    if(m_cnt ==0)
    {
        m_index =0;
        return true;
    }
    m_index = 1;*/

    V v;
    memset(&v,0,sizeof(V));
    inFile.read((char *)&v, sizeof(V));
   
    m_name = v.name;
    m_age = v.age;
    m_man =v.sex;
    m_woman =1-v.sex;

    UpdateData(false);

    return true;   
}

BEGIN_MESSAGE_MAP(C2, CPropertyPage)
    ON_BN_CLICKED(IDC_P, &C2::OnBnClickedP)
    ON_BN_CLICKED(IDC_N, &C2::OnBnClickedN)
    ON_BN_CLICKED(IDC_R, &C2::OnBnClickedR)
END_MESSAGE_MAP()


// C2 消息处理程序

void C2::OnBnClickedR()
{
    ifstream inFile;
    inFile.open ("D:\\a.txt",ios_base::in|ios_base::binary);
   
    V v;
    memset(&v, 0 ,sizeof(V));
    inFile.read((char *)&v, sizeof(V));

    //inFile.close();

    m_name = v.name;
    m_age = v.age;
    m_man = v.sex;
    m_woman = 1-v.sex;

    //GetWindowText(aBText);

    UpdateData(false);
}

void C2::OnBnClickedP()
{
    CString m_name;
    CString w;
    UpdateData(true);
    w =m_name.GetString();
    MessageBox(w,_T(""),MB_OK);
    w.ReleaseBuffer();


    if(m_index == 1|| m_index == 0)
        return;

    inFile.seekg(-2 * sizeof(V),ios_base::cur);

    m_index --;
   
    V v;
    memset(&v,0,sizeof(V));
    inFile.read((char *)&v,sizeof(V));

    m_name = v.name;
    m_age = v.age;
    m_man = v.sex;
    m_woman = 1-v.sex;

     UpdateData(false);
}

void C2::OnBnClickedN()
{
    if(m_cnt == m_index)
        return;

    m_index ++;

    V v;
    memset(&v, 0 ,sizeof(V));
    inFile.read ((char *)&v, sizeof(V));

    m_name = v.name;
    m_age =v.age;
    m_man = v.sex;
    m_woman = 1- v.sex;

    UpdateData(false);

}

[ 本帖最后由 Candicechan 于 2013-6-11 01:08 编辑 ]
搜索更多相关主题的帖子: 压缩包 对话框 public include 
2013-06-11 01:03



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-414955-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.039402 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved