酒店管理程序中的神秘错误-C
我做了一个酒店管理课程。它有一个问题:在Accounts函数中,它应该通过程序重置注册成员的所有标志。但有一个错误是它没有。我花了好几个月的时间,试图调试这个问题,但我做不到。所以请帮帮我。这是account函数的代码酒店管理程序中的神秘错误-C,c,loops,fwrite,fread,C,Loops,Fwrite,Fread,我做了一个酒店管理课程。它有一个问题:在Accounts函数中,它应该通过程序重置注册成员的所有标志。但有一个错误是它没有。我花了好几个月的时间,试图调试这个问题,但我做不到。所以请帮帮我。这是account函数的代码 void accounts() { int ttt=0; struct person payment; char aname[21], oname[21]; char *namea; int chec=1, ver=0; long
void accounts()
{
int ttt=0;
struct person payment;
char aname[21], oname[21];
char *namea;
int chec=1, ver=0;
long int recsize;
recsize=sizeof(payment);
f=fopen("C:\\HOTEL.DAT", "rb+");
if(f == NULL){
clrscr();
cprintf("File could not be opened!");
sleep(4);
exit(0);
}
clrscr();
cprintf("\n *** Pearl Guest House - Payments ***\n\n");
cprintf("\r\r\rEnter the Name :");
fflush(stdin);
scanf("%[^\n]s", &aname);
namea=strupr(aname);
strcpy(oname, namea);
while(fread(&payment, recsize, 1, f) == 1){
if((payment.flag == 1) && (strcmp(payment.name,oname) == 0)){
payment.pay=1;
printf("\n\n Payment Received");
fflush(stdin);
getch();
ver=1;
fseek(f, -recsize, SEEK_CUR);
fwrite(&payment, sizeof(payment), 1, f);
break;
}
}
if(ver!=1){
printf("\n\n Record not Found!!!");
fflush(stdin);
getch();
}
//rewind(f);
fclose(f);
f=fopen("C:\\HOTEL.DAT", "rb+");
if(f == NULL){
clrscr();
cprintf("File could not be opened!");
sleep(4);
exit(0);
}
while(fread(&payment, recsize, 1, f) == 1){
if(payment.pay==0){
chec=0;
break;
}
}
//rewind(f);
f=fopen("C:\\HOTEL.DAT", "rb+");
if(f == NULL){
clrscr();
cprintf("File could not be opened!");
sleep(4);
exit(0);
}
if(chec==1){
while(fread(&payment, recsize, 1, f) == 1){
payment.pay=0;
fseek(f, -recsize, SEEK_CUR);
fwrite(&payment, recsize, 1, f);
ttt++;
printf("%d", ttt);
}
printf("\n\n All payments recieved...\n\nSo, the payments flags are set to 0");
fflush(stdin);
getch();
}
printf("Before Fclose");
fclose(f);
printf("After Fclose");
}
这是我的结构人:
struct s_office{
char name[16];
char phone[12];
};
struct permanent{
char addr[100];
char phone[12];
};
struct emergency{
char name[21];
char relation[11];
char phone[12];
};
struct person{
char name[21];
char phone[12];
char place[21];
int roomno;
int flag;
char food;
struct s_office office;
char father[21];
char fphone[12];
struct permanent per;
struct emergency emer1;
char email[40];
int finger;
char dob[8];
int cidate;
int cimonth;
int ciyear;
int codate;
int comonth;
int coyear;
int rent;
int pay;
};
以下是我包含的标题列表:
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
#include<dos.h>
这里我假设循环是正常的,因为如果我对循环的内容进行注释,它会运行n次。(如果n=条目数)但如果我取消对内容的注释,则不会!这就是问题所在。因此,它不会将它们全部设置为0。
我想要的是这个问题的解决方案和代码,以便它提供所需的输出(将每个薪资变量设置为0)
记住:这是一个14岁的孩子试图编码;请帮助并礼貌详细地回答;;;谢谢
我已将代码移到Visual Studio Express 2012,现在它显示了另一个问题
它在帐户中进入无限循环并创建数千个条目!!!!
这是密码
void accounts()
{
int ttt=0;
struct person payment;
char aname[21], oname[21];
char *namea;
int chec=1, ver=0;
long int recsize;
recsize=sizeof(payment);
f=fopen("D:\\HOTEL.DAT", "rb+");
if(f == NULL){
system("cls");
cprintf("File could not be opened!");
MySleep(4);
exit(0);
}
system("cls");
cprintf("\n *** Pearl Guest House - Payments ***\n\n");
cprintf("\r\r\rEnter the Name :");
fflush(stdin);
scanf("%[^\n]s", &aname);
namea=strupr(aname);
strcpy(oname, namea);
while(fread(&payment, recsize, 1, f) == 1){
if((payment.flag == 1) && (strcmp(payment.name,oname) == 0)){
payment.pay=1;
printf("\n\n Payment Received");
fflush(stdin);
getch();
ver=1;
fseek(f, -recsize, SEEK_CUR);
fwrite(&payment, sizeof(payment), 1, f);
break;
}
}
if(ver!=1){
printf("\n\n Record not Found!!!");
fflush(stdin);
getch();
}
//rewind(f);
fclose(f);
f=fopen("D:\\HOTEL.DAT", "rb+");
if(f == NULL){
system("cls");
cprintf("File could not be opened!");
MySleep(4);
exit(0);
}
while(fread(&payment, recsize, 1, f) == 1){
if(payment.pay==0){
chec=0;
break;
}
}
//rewind(f);
f=fopen("D:\\HOTEL.DAT", "rb+");
if(f == NULL){
system("cls");
cprintf("File could not be opened!");
MySleep(4);
exit(0);
}
if(chec==1){
while(fread(&payment, recsize, 1, f) == 1){
payment.pay=0;
fseek(f, -recsize, SEEK_CUR);
fwrite(&payment, recsize, 1, f);
ttt++;
printf("%d", ttt);
}
printf("\n\n All payments recieved...\n\nSo, the payments flags are set to 0");
fflush(stdin);
getch();
}
printf("Before Fclose");
fclose(f);
printf("After Fclose");
}
您是否已在调试器中运行此代码以查看是否可以确定错误所在?如果不这样做,我们都会在黑暗中冒险 无论如何,我可以看到你有两个可能的问题
- 这段代码无法始终工作:
fwrite
之后调用fflush(f)
fwrite
和flush
都会返回错误,您不会检查错误或处理错误。这也可能是你腐败的根源
- 结构填料(如Jongware在评论中所述)
- 结构中字段之间的填充量
- 添加到结构末端以使其在自然边界上结束的填充量。大多数32位机器喜欢在32位边界上启动数据
我觉得我忘了什么,但是。。。。希望这会让你开始。为什么你会使用Turbo C++?最新版本是7年…我不相信人们仍然写这样的代码并应用到它的C++标签。(1)这是C代码,而不是C++。(2) 试着找一个简短而完整的例子来说明这个问题。(3) 写下你得到了什么输出和你期望得到什么(4)展示你自己在“很多个月”里试图解决问题做了什么,你还可以重写整个程序,这次使用单元测试来检查每个部分是否正常工作。建议1。检查
fseek()
和fwrite()
的结果。2.在fwrite()
之后执行fflush(f)
,并检查fflsuh()
结果。
void accounts()
{
int ttt=0;
struct person payment;
char aname[21], oname[21];
char *namea;
int chec=1, ver=0;
long int recsize;
recsize=sizeof(payment);
f=fopen("D:\\HOTEL.DAT", "rb+");
if(f == NULL){
system("cls");
cprintf("File could not be opened!");
MySleep(4);
exit(0);
}
system("cls");
cprintf("\n *** Pearl Guest House - Payments ***\n\n");
cprintf("\r\r\rEnter the Name :");
fflush(stdin);
scanf("%[^\n]s", &aname);
namea=strupr(aname);
strcpy(oname, namea);
while(fread(&payment, recsize, 1, f) == 1){
if((payment.flag == 1) && (strcmp(payment.name,oname) == 0)){
payment.pay=1;
printf("\n\n Payment Received");
fflush(stdin);
getch();
ver=1;
fseek(f, -recsize, SEEK_CUR);
fwrite(&payment, sizeof(payment), 1, f);
break;
}
}
if(ver!=1){
printf("\n\n Record not Found!!!");
fflush(stdin);
getch();
}
//rewind(f);
fclose(f);
f=fopen("D:\\HOTEL.DAT", "rb+");
if(f == NULL){
system("cls");
cprintf("File could not be opened!");
MySleep(4);
exit(0);
}
while(fread(&payment, recsize, 1, f) == 1){
if(payment.pay==0){
chec=0;
break;
}
}
//rewind(f);
f=fopen("D:\\HOTEL.DAT", "rb+");
if(f == NULL){
system("cls");
cprintf("File could not be opened!");
MySleep(4);
exit(0);
}
if(chec==1){
while(fread(&payment, recsize, 1, f) == 1){
payment.pay=0;
fseek(f, -recsize, SEEK_CUR);
fwrite(&payment, recsize, 1, f);
ttt++;
printf("%d", ttt);
}
printf("\n\n All payments recieved...\n\nSo, the payments flags are set to 0");
fflush(stdin);
getch();
}
printf("Before Fclose");
fclose(f);
printf("After Fclose");
}
while(fread(&payment, recsize, 1, f) == 1){
payment.pay=0;
fseek(f, -recsize, SEEK_CUR);
fwrite(&payment, recsize, 1, f);
ttt++;
printf("%d", ttt);
}