我必须使用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;
答案 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
写入文件,它给出了IO错误...使用字符
答案 1 :(得分:0)
你确定在给定ON_COMMAND(601,save)声明的情况下调用add_dialog :: save()吗?
请检查返回状态,检查fp上的I / O操作是否成功。示例:如果文件打开失败,尝试写入文件将失败。见http://msdn.microsoft.com/en-us/library/3d65ch27(v=VS.80).aspx
因为你标记了这个作业:我建议将CFile包装在一个新类(CStudentDB
?)中。您可以将所有I / O详细信息放在此类中,只编写和调试一次,并为程序的GUI部分提供更简单的界面(例如:while (studentDB.Read(e)) {..}
)。