2011-05-04 103 views
0

我必須用GUI在Visual Basic 6.0中製作一個C++項目。該項目是學生的考試數據庫。該計劃的功能是添加記錄並顯示記錄並打印學生考試的通關單。編輯記錄時崩潰

我做了一些部分,但我無法添加記錄在一個文件db.dat我做了一些菜單和對話框添加刪除和查看記錄,但只有添加對話框打開,它也不工作正確並且不要將數據存儲在文件中,同時刪除或修改記錄程序將會崩潰。我不明白該怎麼辦,幫助我。該程序的代碼如下:

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

struct student 
{ 
    char name[25]; 
    char address[40]; 
    char college[10]; 
    unsigned long int phone_no; 
    unsigned int batch_no; 
    char course[2]; 
    char start_date[10]; 
    char class_timings[15]; 
    char computer_timings[15]; 
    float total_fees; 
    float first_inst; 
    float second_inst; 
    float balance; 
}; 

CFile fp("student.dat",CFile::modeCreate|CFile::modeNoTruncate|CFile::modeReadWrite); 

CString g_name; 
unsigned int g_batchno; 

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

class add_dialog: public CDialog 
{ 
private: 
    struct student e; 
    CString s[7]; 

    public: 

     add_dialog():CDialog(IDD_DIALOG2) 
     { 
      e.phone_no=e.batch_no=0; 
      e.total_fees=e.first_inst=0.0; 
      e.second_inst=e.balance=0.0; 
     } 

     void DoDataExchange(CDataExchange *p) 
     { 
      DDX_Text(p,IDC_EDIT1,s[0]); 
      DDX_Text(p,IDC_EDIT2,s[1]); 
      DDX_Text(p,IDC_EDIT4,s[2]); 
      DDX_Text(p,IDC_EDIT5,e.phone_no); 
      DDX_Text(p,IDC_EDIT6,e.batch_no); 
      DDX_Text(p,IDC_COMBO5,s[3]); 
      DDX_Text(p,IDC_EDIT7,s[4]); 
      DDX_Text(p,IDC_COMBO6,s[5]); 
      DDX_Text(p,IDC_COMBO7,s[6]); 
      DDX_Text(p,IDC_EDIT3,e.total_fees); 
      DDX_Text(p,IDC_EDIT8,e.first_inst); 
      DDX_Text(p,IDC_EDIT9,e.second_inst); 
      DDX_Text(p,IDC_EDIT10,e.balance); 
     } 

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

      strcpy(e.name,s[0]); 
      strcpy(e.college,s[1]); 
      strcpy(e.address,s[2]); 
      strcpy(e.course,s[3]); 
      strcpy(e.start_date,s[4]); 
      strcpy(e.class_timings,s[5]); 
      strcpy(e.computer_timings,s[6]); 

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

    DECLARE_MESSAGE_MAP() 
}; 

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

class getname_dialog: public CDialog 
{ 
public: 
    getname_dialog(): CDialog(IDD_DIALOG3) 
    { 
     g_name=""; 
     g_batchno=0; 
    } 

    void DoDataExchange(CDataExchange *p) 
    { 
     DDX_Text(p,IDC_EDIT1,g_batchno); 
     DDX_Text(p,IDC_EDIT2,g_name); 
    } 
}; 

class modify_dialog: public CDialog 
{ 
private: 

    CString s[7]; 
    struct student e; 

    public: 

     modify_dialog(struct student ee):CDialog (IDD_DIALOG2) 
     { 
      e=ee; 
      s[0]=ee.name; 
      s[1]=ee.college; 
      s[2]=ee.address; 
      s[3]=ee.course; 
      s[4]=ee.start_date; 
      s[5]=ee.class_timings; 
      s[6]=ee.computer_timings; 
     } 

     void DoDataExchange(CDataExchange *p) 
     { 
      DDX_Text(p,IDC_EDIT1,s[0]); 
      DDX_Text(p,IDC_EDIT2,s[1]); 
      DDX_Text(p,IDC_EDIT4,s[2]); 
      DDX_Text(p,IDC_EDIT5,e.phone_no); 
      DDX_Text(p,IDC_EDIT6,e.batch_no); 
      DDX_Text(p,IDC_COMBO5,s[3]); 
      DDX_Text(p,IDC_EDIT7,s[4]); 
      DDX_Text(p,IDC_COMBO6,s[5]); 
      DDX_Text(p,IDC_COMBO7,s[6]); 
      DDX_Text(p,IDC_EDIT3,e.total_fees); 
      DDX_Text(p,IDC_EDIT8,e.first_inst); 
      DDX_Text(p,IDC_EDIT9,e.second_inst); 
      DDX_Text(p,IDC_EDIT10,e.balance); 
     } 

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

      strcpy(e.name,s[0]); 
      strcpy(e.college,s[1]); 
      strcpy(e.address,s[2]); 
      strcpy(e.course,s[3]); 
      strcpy(e.start_date,s[4]); 
      strcpy(e.class_timings,s[5]); 
      strcpy(e.computer_timings,s[6]); 

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

DECLARE_MESSAGE_MAP() 
}; 

BEGIN_MESSAGE_MAP(modify_dialog,CDialog) 
ON_COMMAND(601,save) 
END_MESSAGE_MAP() 

class myframe: public CFrameWnd 
{ 
public: 
    myframe() 
    { 
     CString mywindowclass; 

     CBrush mybrush; 
     mybrush.CreateSolidBrush(RGB(225,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 student e; 

     CClientDC d(this); 
     CRect r; 
     int y; 

     char str[80]; 
     fp.SeekToBegin(); 
     y=0; 

     GetClientRect(&r); 

     CBrush mybrush (RGB(255,255,255)); 

     d.FillRect(&r,&mybrush); 

     while(fp.Read(&e,sizeof(e))>=sizeof(e)) 
     { 
      sprintf(str,"%-25s %-40s",e.name,e.address); 

      d.TextOut(0,y,str,strlen(str)); 

      y+=15; 
     } 
    } 

    void college() 
    { 
     struct student e; 

     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(&e,sizeof(e))>=sizeof(e)) 
     { 
      sprintf(str,"%-25s %-20s %-6u %-10s",e.name, e.course,e.batch_no,e.college); 
      d.TextOut(0,y,str,strlen(str)); 
      y+=15; 
     } 

    } 

    void batch() 
    { 
     struct student e; 

     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(&e,sizeof(e))>=sizeof(e)) 
     { 
      sprintf(str,"%-25s %-20s %-15s %-15s",e.name,e.course,e.class_timings,e.computer_timings); 
      d.TextOut(0,y,str,strlen(str)); 
      y+=15; 
     } 
    } 

    void defaulters() 
    { 
     struct student e; 

     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(&e,sizeof(e))>=sizeof(e)) 
     { 
      if(e.balance>0) 
      { 
       sprintf(str,"%-25s %-20s %-6.6f %-6.6f %-6.6f",e.name,e.course,e.first_inst,e.second_inst,e.balance); 
       d.TextOut(0,y,str,strlen(str)); 
       y+=15;  
      } 
     } 
    } 

    void modifyrec() 
    { 
     Invalidate(); 

     bool found; 
     struct student e; 

     getname_dialog diag; 

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

      while(fp.Read(&e,sizeof(e))>=sizeof(e)) 
      { 
       if(e.batch_no==g_batchno && strcmp(e.name,g_name)==0) 
       { 
        found=true; 
        break; 
       } 
      } 

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

    void delrec() 
    { 
     bool found; 
     struct student e; 

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

      CFile ft("temp.dat",CFile::modeCreate|CFile::modeWrite); 

      while(fp.Read(&e,sizeof(e))>=sizeof(e)) 
      { 
       if(e.batch_no==g_batchno && strcmp(e.name,g_name)==0) 

        found=true; 
       else 
        ft.Write(&e,sizeof(e)); 
      } 
      if(found==false) 
       MessageBox("Record Not Found","Delete Record"); 
      fp.Close(); 
      ft.Close(); 

      CFile::Remove("students.dat"); 
      CFile::Rename("temp.dat","students.dat"); 
      fp.Open("student.dat",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,college) 
ON_COMMAND(303,batch) 
ON_COMMAND(304,defaulters) 
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

這是很多代碼。如何將代碼限制在相關部分?你得到的錯誤是什麼? – 2011-05-04 08:36:42

+1

你應該真的展示一個你正在使用的確切問題的測試用例,而不是發佈你的所有代碼。 – 2011-05-04 08:37:04

+0

並告訴我們,當你遇到這次事故時,程序會說什麼。 – 2011-05-04 08:38:23

回答

0
Object P; 
      int pos=sizeof(Object)*PID-sizeof(Object); 
      f.open("File.dat",ios::in|ios::out|ios::binary|ios::ate); 
      f.seekp(0,ios::beg); 
      f.seekp(pos,ios::cur); 
      f.read((char*)&P,sizeof(Object)); 
      f.close(); 
      return 0; 
在這種方法我寫的對象......它很容易搜索和檢索數據..try這種做法的文件

..also不寫strings它給IO錯誤的文件...使用字符數組到

+0

您是否編輯了上述內容用於顯示您要添加代碼的位置或如何使用字符數組的代碼 – samii 2011-05-04 09:01:49

+0

您的場景是什麼? – Sudantha 2011-05-04 10:18:32

+0

如果你寫字符數組什麼是你計劃閱讀? – Sudantha 2011-05-04 10:20:10

0

你確定add_dialog :: save()被調用,給定的ON_COMMAND(601,保存)聲明?

請檢查fp上的I/O操作是否成功,方法是檢查返回狀態。例如:如果文件打開失敗,嘗試寫入文件將失敗。見http://msdn.microsoft.com/en-us/library/3d65ch27(v=VS.80).aspx

既然你標記了這個家庭作業:我會建議把CFile包裝在一個新的類(CStudentDB?)。您可以將所有I/O詳細信息放在此類中,僅編寫和調試一次,併爲程序的GUI部分提供更簡單的接口(例如:while (studentDB.Read(e)) {..})。