未处理的异常 MSVCRTD.DLL

发布于 2024-11-30 10:35:17 字数 7789 浏览 0 评论 0原文

我在 Visual Studio 6.0 中编写了一个程序,它是一个图形用户界面。我正在使用 MFC 类作为 GUI,它存储书籍记录。在该程序中,当我尝试查看记录时,出现未处理的异常访问冲突错误。我通过 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;

I write a programe in visual studio 6.0 it is a graphical user interface. i am using MFC classes for GUI it store records for books. In that programe i got a unhandled exception Access violation error while i tried to view the records. I save records in a file using file handling thorugh CFile class. When i press OK button on the error message box it lead me to a browsing box asking for a file name as OUTPUT.C but i cannot found such kind of file and when i click Cancel button on this browse box it will take me to the Disassembly file pointing an arrow to this line 1021674C movsx edx,byte ptr [ecx]. I want your help desprately this is my final assignment and i have to submit it tommorrow morning please help me out.
i hope i describe problem correctly so you guys can understand it properly please help me out.
Code of the programe is below :

    #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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心安伴我暖 2024-12-07 10:35:17

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

例如:

class A
{
public:
    int num;
};

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

    return 0;
}

我建议你查看调用堆栈,找出 NULL 指针在哪里。

Access violation means you used a NULL pointer.

for instance:

class A
{
public:
    int num;
};

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

    return 0;
}

I suggest you look at the call stack, and find out where is the NULL pointer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文