2011-08-21 168 views
-1

我在visual studio 6.0中編寫了一個程序,它是一個圖形用戶界面。我使用MFC類來存儲圖書的記錄。在那個程序中,我嘗試查看記錄時遇到了未處理的異常訪問衝突錯誤。我使用文件處理CFile類將文件保存在文件中。當我按下錯誤消息框上的確定按鈕時,它導致我到一個瀏覽框要求文件名稱爲OUTPUT.C,但我找不到這樣的文件,當我點擊此瀏覽框上的取消按鈕,它會帶我到反彙編文件指向此行的箭頭1021674C movsx edx,byte ptr [ecx]。我希望得到你的幫助,這是我的最終任務,我必須在明天上午提交它,請幫助我。 我希望我能正確描述問題,所以你們可以正確理解它,請幫助我。在PROGRAME的 代碼如下:未處理的異常MSVCRTD.DLL

#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

你的程序崩潰在標準庫的內部,Winows知道發生崩潰的文件是output.c,但它不知道該文件在哪裏,所以它會問你。但那並不重要。按照您的方法取消取消,然後查找**堆棧跟蹤**或**調用堆棧**,以顯示您的程序崩潰的位置。當你走上棧跟蹤時,你會從庫代碼返回到你的代碼。這會讓你更好地瞭解崩潰發生的地點和原因。如果您可以在代碼中找到發生此次崩潰的行,則可能有人會幫助您。 – john

+0

這\t 0x002f1940 {myframe hWnd = 0x000b0684}看到這行,如果它可能對你有幫助 – sam

+0

這沒有幫助。你必須在調用堆棧中找到代碼中的某個地方。至少我們需要在代碼中發生崩潰的行號。但你最好的機會是學習如何爲你自己使用調試器。很難通過互聯網進行調試會話。 – john

回答

0

訪問衝突意味着你使用一個NULL指針。

例如:

class A 
{ 
public: 
    int num; 
}; 

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

    return 0; 
} 

我建議你看看調用堆棧,並找出是NULL指針。