Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
酒店管理程序中的神秘错误-C_C_Loops_Fwrite_Fread - Fatal编程技术网

酒店管理程序中的神秘错误-C

酒店管理程序中的神秘错误-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

我做了一个酒店管理课程。它有一个问题:在Accounts函数中,它应该通过程序重置注册成员的所有标志。但有一个错误是它没有。我花了好几个月的时间,试图调试这个问题,但我做不到。所以请帮帮我。这是account函数的代码

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");
}

您是否已在调试器中运行此代码以查看是否可以确定错误所在?如果不这样做,我们都会在黑暗中冒险

无论如何,我可以看到你有两个可能的问题

  • 这段代码无法始终工作:
while(fread(&payment,recsize,1,f)==1){ 付款。付款=0; fseek(f,-recsize,SEEK_CUR); fwrite(和payment,recsize,1,f); ttt++; printf(“%d”,ttt); } 在注释中所述的chux,您应该在
fwrite
之后调用
fflush(f)
fwrite
flush
都会返回错误,您不会检查错误或处理错误。这也可能是你腐败的根源

  • 结构填料(如Jongware在评论中所述)
结构包装意味着两件事:

  • 结构中字段之间的填充量
  • 添加到结构末端以使其在自然边界上结束的填充量。大多数32位机器喜欢在32位边界上启动数据
有些机器无法处理未对齐的字段,需要一些黑客技术,例如,32位int应该在32位对齐的边界上开始,在32位对齐的边界上结束。我可以想到至少有一个处理器使用页面错误来处理这种情况,这大大降低了代码的速度。页面错误比直接内存读写慢一个数量级,因此结构打包变得非常重要

在英特尔x86处理器上,读/写不对齐的惩罚不是很高,因此关闭结构打包在某种程度上是安全的。看一下Type C++文档的语法。这将引导您阅读理解问题所需的内容。一旦您理解了编译器文档,就可以在internet上搜索并阅读有关结构填充/打包的内容

确保每个结构只有一个定义


我觉得我忘了什么,但是。。。。希望这会让你开始。

为什么你会使用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); }