未处理的异常MSVCRTD.DLL

时间:2011-08-21 15:01:41

标签: c++ visual-studio mfc

我在visual studio 6.0中编写了一个程序,它是一个图形用户界面。我正在使用用于GUI的MFC类来存储书籍的记录。在那个程序中,当我试图查看记录时,我得到了一个未处理的异常访问冲突错误。我使用文件处理过程CFile类将记录保存在文件中。当我按错误信息框上的确定按钮时,它会引导我到一个浏览框,询问文件名为OUTPUT.C但我找不到这种文件,当我点击此浏览框上的取消按钮时,它将带我到将箭头指向此行1021674C movsx edx,byte ptr [ecx]的反汇编文件。我想要你的帮助,这是我最后的任务,我必须提交它,明天早上请帮助我。 我希望我正确描述问题所以你们可以理解它,请帮助我。 程序代码如下:

    #include "afxwin.h"
#include "resource.h"

struct books
{
    char bookname[25];
    char authorname[40];
    float price;
    int copies;
    unsigned int ispn;
    int page;
    char issue_date[15];
    char avail[10];

};

CFile fp("book.txt",CFile::modeCreate|CFile::modeNoTruncate|CFile::modeReadWrite);
CString b_name;
unsigned int b_ispn;

class about_dialog:public CDialog
{
public:
    about_dialog():CDialog(IDD_DIALOG1)
    {
    }

};

class add_dialog:public CDialog
{
private:
    struct books b;
    CString s[4];
    public:

        add_dialog():CDialog(IDD_DIALOG2)
        {
            b.price =0.0;
            b.copies = b.page = 0;
            b.ispn = 0 ;

        }

        void DoDataExchange(CDataExchange *p)
        {
            DDX_Text(p,IDC_EDIT1,s[0]);//name
            DDX_Text(p,IDC_EDIT2,s[1]);//author
            DDX_Text(p,IDC_EDIT3,b.copies);//copies
            DDX_Text(p,IDC_EDIT4,b.ispn);//ispn
            DDX_Text(p,IDC_EDIT5,b.price);//price
            DDX_Text(p,IDC_EDIT6,b.page);//pages

            DDX_Text(p,IDC_COMBO5,s[2]);//issu date
            DDX_Text(p,IDC_COMBO6,s[3]);//availabel

        }
        void save()
        {
            CDialog::OnOK();

            strcpy(b.bookname,s[0]);
            strcpy(b.authorname,s[1]);
            strcpy(b.issue_date,s[2]);
            strcpy(b.avail,s[3]);


            fp.SeekToEnd();
            fp.Write(&b,sizeof(b));
        }

        DECLARE_MESSAGE_MAP()
};

BEGIN_MESSAGE_MAP(add_dialog,CDialog)
ON_COMMAND(IDSAVE,save)
END_MESSAGE_MAP()

class getbook_dialog:public CDialog
{
public:

    struct books b;
    getbook_dialog():CDialog(IDD_DIALOG3)
    {
        b_name="";
        b_ispn=0;
    }

    void DoDataExchange(CDataExchange *p)
    {

        DDX_Text(p,IDC_EDIT1,b_ispn);
        DDX_Text(p,IDC_EDIT2,b_name);
    }

};

class modify_dialog:public CDialog
{
private:
    CString s[4];
    struct books b;

    public:

        modify_dialog(struct books bb):CDialog(IDD_DIALOG2)
        {
            b=bb;
            s[0]=bb.bookname;
            s[1]=bb.authorname;
            s[2]=bb.issue_date;
            s[3]=bb.avail;


        }


    void DoDataExchange(CDataExchange *p)
        {
            DDX_Text(p,IDC_EDIT1,s[0]);//name
            DDX_Text(p,IDC_EDIT2,s[1]);//author
            DDX_Text(p,IDC_EDIT3,b.copies);//copies
            DDX_Text(p,IDC_EDIT4,b.ispn);//ispn
            DDX_Text(p,IDC_EDIT5,b.price);//price
            DDX_Text(p,IDC_EDIT6,b.page);//pages
            DDX_Text(p,IDC_COMBO5,s[2]);//issu date
            DDX_Text(p,IDC_COMBO6,s[3]);//availabel

        }
        void save()
        {
            CDialog::OnOK();

            strcpy(b.bookname,s[0]);
            strcpy(b.authorname,s[1]);
            strcpy(b.issue_date,s[2]);
            strcpy(b.avail,s[3]);

            fp.Seek(-(long)sizeof(b),CFile::current);
            fp.Write(&b,sizeof(b));

        }

        DECLARE_MESSAGE_MAP()
};
BEGIN_MESSAGE_MAP(modify_dialog,CDialog)
ON_COMMAND(IDSAVE,save)
END_MESSAGE_MAP()

class myframe: public CFrameWnd
{
public:

    myframe()
    {
        CString mywindowclass;
        CBrush mybrush;
        mybrush.CreateSolidBrush(RGB(255,255,255));

        mywindowclass = AfxRegisterWndClass(CS_HREDRAW |CS_VREDRAW,AfxGetApp()->LoadStandardCursor(IDC_ARROW),mybrush,AfxGetApp()->LoadIcon(IDI_ICON1));

        Create(mywindowclass,"DATABASE",WS_OVERLAPPEDWINDOW,rectDefault,0,MAKEINTRESOURCE(IDR_MENU1));

    }

    void about()
    {
        about_dialog diag;
        diag.DoModal();
    }

    void addrec()
    {
        Invalidate();
        add_dialog diag;
        diag.DoModal();
    }

    void byname()
    {
        struct books b;

        CClientDC d(this);
        CRect r;
        int y;
        char str[90];
        fp.SeekToBegin();
        y=0;
        GetClientRect(&r);
        CBrush mybrush(RGB(255,255,255));
        d.FillRect(&r,&mybrush);

        while(fp.Read(&b,sizeof(b))>=sizeof(b))
        {
            sprintf(str,"%-25s %-40s",b.bookname,b.copies);
            d.TextOut(0,y,str,strlen(str));

            y+=15;
        }
    }

    void author()
    {
        struct books b;

        CClientDC d(this);
        int y;
        char str[90];
        CRect r;

        GetClientRect(&r);
        CBrush mybrush(RGB(255,255,255));
        d.FillRect(&r,&mybrush);

        fp.SeekToBegin();
        y=0;

        while(fp.Read(&b,sizeof(b))>=sizeof(b))
        {
            sprintf(str,"%-25s %-20s %-6u %-10s",b.bookname,b.authorname,b.page,b.copies);
            d.TextOut(0,y,str,strlen(str));
            y+=15;
        }
    }

    void ispn()
    {
        struct books b;
        CClientDC d(this);
        int y;
        char str[90];
        CRect r;
        GetClientRect(&r);
        CBrush mybrush (RGB(255,255,255));
        d.FillRect(&r,&mybrush);

        fp.SeekToBegin();
        y=0;

        while(fp.Read(&b,sizeof(b))>=sizeof(b))
        {
            sprintf(str,"%-25s %-20s %-15s %-15s",b.bookname,b.authorname,b.ispn,b.avail);
        //  d.TextOut(0,y,str,strlen(str));
            y+=15;
        }
    }


    void modifyrec()
    {
        Invalidate();

        bool found;

        struct books b;

        getbook_dialog diag;
        if(diag.DoModal()==IDOK)
        {
            found = false;

            fp.SeekToBegin();

            while(fp.Read(&b,sizeof(b))>=sizeof(b))
            {
                if(b.ispn == b_ispn && strcmp(b.bookname,b_name)==0)
                {
                    found = true;
                    break;
                }
            }

            if(found == true )
            {
                modify_dialog mdiag(b);
                mdiag.DoModal();
            }
            else
                MessageBox("Record Not Found","Modify Record....");
        }
    }

    void delrec()
    {
        bool found;
        struct books b;

        Invalidate();

        getbook_dialog diag;
        if(diag.DoModal()==IDOK)
        {
            found = false;
            fp.SeekToBegin();

            CFile ft("temp.dat",CFile::modeCreate | CFile::modeWrite);
            while(fp.Read(&b,sizeof(b))>= sizeof(b))
            {
                if(b.ispn==b_ispn && strcmp(b.bookname,b_name)==0)
                {
                    found = true;
                }
                else 
                    ft.Write(&b,sizeof(b));

            }

            if(found == false )
                    MessageBox("Record Not Found","Delete Record....");

            fp.Close();
            ft.Close();

            CFile::Remove("book.txt");
            CFile::Rename("temp.dat","book.txt");
            fp.Open("book.txt",CFile::modeCreate |CFile::modeNoTruncate |CFile::modeReadWrite);
        }
    }

    DECLARE_MESSAGE_MAP()
};
BEGIN_MESSAGE_MAP(myframe,CFrameWnd)

ON_COMMAND(101,about)
ON_COMMAND(201,addrec)
ON_COMMAND(301,byname)
ON_COMMAND(302,author)
ON_COMMAND(303,ispn)
ON_COMMAND(401,modifyrec)
ON_COMMAND(501,delrec)

END_MESSAGE_MAP()


class myapp:public CWinApp
{
    public:

        int InitInstance()
        {
            myframe *fr;
            fr = new myframe;
            fr->ShowWindow(SW_SHOWMAXIMIZED);
            m_pMainWnd = fr;

            return 1;
        }
};
myapp app;

1 个答案:

答案 0 :(得分:0)

访问冲突意味着您使用了NULL指针。

例如:

class A
{
public:
    int num;
};

int main()
{
    A *pa = NULL;
    cout << pa->num << endl; // <----------------access violation

    return 0;
}

我建议你查看调用堆栈,找出NULL指针的位置。