2011-05-04 3 views
0

GUI가있는 Visual Basic 6.0에서 C++ 프로젝트를 만들어야합니다. 이 프로젝트는 학생들을위한 시험 데이터베이스입니다. 이 프로그램의 기능은 기록을 추가하고 기록을 보여주고 학생 시험을위한 통관 표를 인쇄하는 것입니다.레코드 편집시 크래시

나는 일부분을 만들었지 만 db.dat 파일에 레코드를 추가 할 수 없었습니다. 레코드를 삭제하고보고 있지만 추가 대화 상자를 열고 추가 할 수있는 대화 상자를 만들었습니다. 제대로 기록 프로그램을 삭제하거나 수정하는 동안 파일에 데이터를 저장하지 마십시오. 나는 PLZ가 나에게 도움이 될 것을 이해하지 못한다. 프로그램의 코드는 다음과 같습니다 :

#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; 
... 쉬운 검색 및 검색 데이터가이 방법을 ..try 파일이 접근 메신저 쓰기 개체에서
+1

많은 코드가 있습니다. 코드를 관련 부분으로 제한하는 것은 어떻습니까? 당신이 얻는 오류는 무엇입니까? –

+1

코드를 게시하는 대신 정확한 문제에 대한 테스트 케이스를 보여줘야합니다. –

+0

충돌이 발생할 때 프로그램에서 정확히 무엇을 말합니까? –

답변

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; 

strings을 ..also를 쓰지 않는다 파일에 IO 오류가 발생했습니다 ... 문자 배열을 사용합니다

+0

위 편집을 수행 했습니까? 코드를 추가 할 위치 또는 문자 배열을 사용하는 방법을 나타내는 코드 – samii

+0

은 어떤 시나리오입니까? – Sudantha

+0

당신이 char 배열을 쓰는 경우 읽으려는 계획이 뭐죠? – Sudantha

0

ON_COMMAND (601, 저장) 선언이 주어지면 add_dialog :: save()가 호출됩니까?

반환 상태를 확인하여 fp에 대한 I/O 작업이 성공했는지 확인하십시오. 예 : 파일 열기가 실패하면 쓰기 시도가 비참하게 실패합니다. http://msdn.microsoft.com/en-us/library/3d65ch27(v=VS.80).aspx

그리고이 숙제에 태그를 추가 했으므로 : 새로운 클래스 (CStudentDB?) 안에 CFile을 배치하는 것이 좋습니다. 모든 I/O 세부 정보를이 클래스에 넣고 한 번만 작성하고 디버그하고 프로그램의 GUI 부분에 더 쉬운 인터페이스 (예 : while (studentDB.Read(e)) {..})를 제공 할 수 있습니다.