C上的酒店管理系统没有错误,但也不会运行

时间:2017-04-28 09:19:51

标签: c

我正在C上为酒店管理系统做这个项目。我写了代码,但它仍然不会运行并显示“警告:指针和整数之间的比较。 我正在学习的过程中,所以我真的很喜欢这里的菜鸟。

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

struct booking
{
int customer_id;
char name[50];
int roomno;
int dob_day,dob_month,dob_year;
int days;
int chargeperday;
int amount;
};

struct expense
{
int customer_id;
int doe_day,doe_month,doe_year;
char comments[100];
int amount;
};

int check_existence(int customer_id)
{
FILE *fp;
int f;
int flag=0;
struct booking b;
fp=fopen("booking.dat","r");
if(fp==NULL)
{
    return 0;
}
while(1)
{
    f=fread(&b,sizeof(struct booking),1,fp);
    if(f==0)
    {
        break;
    }
    if(b.customer_id==customer_id)
    {
        flag=1;
    }
}
fclose(fp);
return flag;
}

void book_room(struct booking b,char response[])
{
if(check_existence(b.customer_id)==1)
{
    strcpy(response,"1002-Customer Id Already Exists");
    return;
}
FILE *fp;
fp=fopen("booking.dat","ab");
if(fp==NULL)
{
    strcpy(response,"1000-Unable to Open Booking File");
    return;
}
fwrite(&b,sizeof(struct booking),1,fp);
strcpy(response,"1001-Room Booked Successfully");
fclose(fp);
}

void unbook_room(int customer_id,char response[])
{
FILE *fp1,*fp2;
int flag=0,f;
struct booking b;
fp1=fopen("booking.dat","rb");
if(fp1==NULL)
{
    strcpy(response,"1003-Room has not been booked for this customer");
    return;
}
fp2=fopen("temp.dat","wb");
if(fp2==NULL)
{
    strcpy(response,"1004-Temporary file could not be opened");
    return;
}
while(1)
{
    f=fread(&b,sizeof(struct booking),1,fp1);
    if(f==NULL)
    { break; }
    if(b.customer_id!=customer_id)
    {
        fwrite(&b,sizeof(struct booking ),1,fp2);
    }
    else
    {
        flag=1;
    }
}
fclose(fp1);
fclose(fp2);
if(flag==1)
{
    strcpy(response,"1005-Room Unbooked Successfully");
}
else
{
    strcpy(response,"1003-Room Has Not Been Booked for this customer");
}
remove("booking.dat");
rename("temp.dat","booking.dat");
}



void get_expense_list(int customer_id,char response[])
{
int flag,f;
FILE *fp1,*fp2;
struct expense e;
fp1=fopen("expense.dat","rb");
if(fp1==NULL)
{
    strcpy(response,"1008-Expense File Could Not Be Opened");
    return;
}
fp2=fopen("temp.dat","wb");
if(fp2==NULL)
{
    strcpy(response,"1004-Temporary File Could Not Be Opened");
    return;
}
flag=0;
while(1)
{
    f=fread(&e,sizeof(struct expense
),1,fp1);
    if(f==NULL)
    {   break;  }
    if(e.customer_id==customer_id)
    {
        fwrite(&e,sizeof(struct expense
),1,fp2);
        flag=1;
    }
}
fclose(fp1);
fclose(fp2);
if(flag==1)
{
    strcpy(response,"1009-Expense Details Retrieved Successfully");
}
else
{
    strcpy(response,"1010-No Extra Expenses Made By This User");
}
}

void get_booking_list(int customer_id,char response[])
{
int flag,f;
FILE *fp1,*fp2;
struct booking b;
fp1=fopen("booking.dat","rb");
if(fp1==NULL)
{
    strcpy(response,"1000-Booking File Could Not Be Opened");
    return;
}
fp2=fopen("temp.dat","wb");
if(fp2==NULL)
{
    strcpy(response,"1004-Temporary File Could Not Be Opened");
    return;
}
flag=0;
while(1)
{
    f=fread(&b,sizeof(b),1,fp1);
    if(f==NULL)
    {   break;  }
    if(b.customer_id==customer_id)
    {
        fwrite(&b,sizeof(b),1,fp2);
        flag=1;
    }
}
fclose(fp1);
fclose(fp2);
if(flag==1)
{
    strcpy(response,"1012-Booking Details Retrieved Successfully");
}
else
{
    strcpy(response,"1013-No Booking Details Exist For This Customer Id");
}
}

float get_booking_expense(int customer_id,char response[])
{
int flag,f;
float sum=0;
FILE *fp;
struct booking b;
fp=fopen("booking.dat","rb");
if(fp==NULL)
{
    strcpy(response,"1000-Booking File Could Not Be Opened");
    return 0.0F;
}
while(1)
{
    f=fread(&b,sizeof(b),1,fp);
    if(f==NULL)
    {   break;  }
    if(b.customer_id==customer_id)
    {   sum+=b.amount;  }
}
fclose(fp);
strcpy(response,"1011-Booking Sum Calculated Successfully");
return sum;
}

void delete_expense_list(int customer_id,char response[])
{
FILE *fp1,*fp2;
int flag=0,f;
struct expense e;
fp1=fopen("expense.dat","rb");
if(fp1==NULL)
{
    strcpy(response,"1014-No Expense Details Exist For This Customer");
    return;
}
fp2=fopen("temp.dat","wb");
if(fp2==NULL)
{
    strcpy(response,"1004-Temporary file could not be opened");
    return;
}
while(1)
{
    f=fread(&e,sizeof(e),1,fp1);
    if(f==NULL)
    { break; }
    if(e.customer_id!=customer_id)
    {
        fwrite(&e,sizeof(e),1,fp2);
    }
    else
    {
        flag=1;
    }
}
fclose(fp1);
fclose(fp2);
if(flag==1)
{
    strcpy(response,"1015-Expense Details Removed Successfully");
}
else
{
    strcpy(response,"1014-No Expense Details Exist for This Custoemr");
}
remove("expense.dat");
rename("temp.dat","expense.dat");
}





void send_file(int count)
{
char response[1500];
FILE *fp;
fp=fopen("response.dat","rb");
if(fp==NULL)
{
    printf("\nRequest could not be sent to client");
    return;
}
int i=0;
int f;
char ch;

while(1)
{
    f=fread(&ch,1,1,fp);
    if(f==NULL)
    {
        break;
    }
    response[i++]=ch;
}
fclose(fp);

}

float get_expense_sum(int customer_id,char response[])
{
int flag,f;
float sum=0;
FILE *fp;
struct expense e;
fp=fopen("expense.dat","rb");
if(fp==NULL)
{
    strcpy(response,"1008-Expense File Could Not Be Opened");
    return 0.0F;
}
while(1)
{
    f=fread(&e,sizeof(e),1,fp);
    if(f==NULL)
    {   break;  }
    if(e.customer_id==customer_id)
    {   sum+=e.amount;  }
}
fclose(fp);
strcpy(response,"1009-Expense Sum Calculated Successfully");
return sum;
}

void main()
{
int count;

struct booking b;
struct expense e;

int customer_id;

float bbill;
float ebill;

char request[1000];
char response[1500];
FILE *fp,*fp1;
int request_no;
int response_no;
int i,f;
char ch;


  printf("\n\nt\t\t******* HOTEL MANAGEMENT SERVER ******\n");

  printf("\nRequest No.= ");
  scanf("%d", &request_no);

  switch(request_no)
  {
    case 2000:
        // case to book a room
        fread(&b,sizeof(b),1,fp);
        printf("\n\nRequest For Room Book Containing Following Data :\n");
        printf("\nCustomer Id : %d",b.customer_id);
        printf("\nName : %s",b.name);
        printf("\nRoom No. : %d",b.roomno);
        printf("\nDate of Booking : %d-%d-
%d",b.dob_day,b.dob_month,b.dob_year);
        printf("\nDays : %d",b.days);
        printf("\nCharge Per Day : %d",b.chargeperday);
        printf("\nAmount : %d",b.amount);

        book_room(b,response);

        printf("\n\nResponse Sent=%s\n",response);
        fclose(fp);
        fp=fopen("response.dat","wb");
        if(fp==NULL)
        {
            printf("\nResponse could not be sent");

        }
        fwrite(response,1,strlen(response),fp);
        fclose(fp);
        send_file(strlen(response));

    case 2001:

        count=0;
        fread(&customer_id,sizeof(customer_id),1,fp);
        printf("\n\nRequest for BOOKING LIST for Customer Id : 
%d",customer_id);
        fclose(fp);
        get_booking_list(customer_id,response);

        printf("\n\nResponse Sent=%s\n",response);

        fp1=fopen("response.dat","wb");
        if(fp1==NULL)
        {
            printf("\nUnable to open response file");

        }
        if(strstr(response,"1004")!=0)
        {
            response_no=1004;
            fwrite(&response_no,sizeof(response_no),1,fp1);
            fclose(fp1);

        }
        if(strstr(response,"1000")!=0)
        {
            response_no=1000;
            fwrite(&response_no,sizeof(response_no),1,fp1);
            fclose(fp1);

        }
        if(strstr(response,"1013")!=0)
        {
            response_no=1013;
            fwrite(&response_no,sizeof(response_no),1,fp1);
            fclose(fp1);

        }
        if(strstr(response,"1012")!=0)
        {
            fp=fopen("temp.dat","rb");
            if(fp==NULL)
            {
                printf("\nUnable to open temporary file");

            }
            response_no=1012;
            fwrite(&response_no,sizeof(response_no),1,fp1);

            while(1)
            {
                f=fread(&ch,1,1,fp);
                if(f==NULL)
                {
                    break;
                }
                fwrite(&ch,1,1,fp1);
                ++count;
            }
            fclose(fp);
        }
        fclose(fp1);

        send_file(count);

    case 2002:
        count=0;
        fread(&customer_id,sizeof(customer_id),1,fp);

        printf("\n\nRequest for EXPENSE LIST for Customer Id : %d",customer_id);

        fclose(fp);

        get_expense_list(customer_id,response);

        printf("\n\nResponse Sent=%s\n",response);

        fp1=fopen("response.dat","wb");
        if(fp1==NULL)
        {
            printf("\nUnable to open response file");

        }
        if(strstr(response,"1008")!=0)
        {
            response_no=1008;
            fwrite(&response_no,sizeof(response_no),1,fp1);
            fclose(fp1);

        }
        if(strstr(response,"1004")!=0)
        {
            response_no=1004;
            fwrite(&response_no,sizeof(response_no),1,fp1);
            fclose(fp1);

        }
        if(strstr(response,"1010")!=0)
        {
            response_no=1010;
            fwrite(&response_no,sizeof(response_no),1,fp1);
            fclose(fp1);

        }
        if(strstr(response,"1009")!=0)
        {
            fp=fopen("temp.dat","rb");
            if(fp==NULL)
            {
                printf("\nUnable to open temporary file");

            }
            response_no=1009;
            fwrite(&response_no,sizeof(response_no),1,fp1);

            while(1)
            {
                f=fread(&ch,1,1,fp);
                if(f==NULL)
                {
                    break;
                }
                fwrite(&ch,1,1,fp1);
                ++count;
            }
            fclose(fp);
        }
        fclose(fp1);

        send_file(count);


    case 2003 :
        fread(&customer_id,sizeof(customer_id),1,fp);

        printf("\n\nRequest for TOTAL BILL for Customer Id : %d",customer_id);

        fclose(fp);

        bbill=get_booking_expense(customer_id,response);
        ebill=get_expense_sum(customer_id,response);

        printf("\n\nResponse Sent=%f Booking Bill, %f Expenses Bill, %f Total Bill\n",bbill,ebill,ebill+bbill);

        fp=fopen("response.dat","wb");
        if(fp==NULL)
        {
            printf("\nUnable to open response file");

        }
        response_no=1000;
        fwrite(&response_no,sizeof(response_no),1,fp);
        fwrite(&bbill,sizeof(bbill),1,fp);
        fwrite(&ebill,sizeof(ebill),1,fp);
        fclose(fp);
        send_file(sizeof(response_no)+sizeof(bbill)+sizeof(ebill));

        case 2004 :
        fread(&customer_id,sizeof(customer_id),1,fp);
        printf("\n\nRequest For Room Checkout with Customer Id : %d",customer_id);
        unbook_room(customer_id,response);
        printf("\n\nResponse Sent=%s\n",response);
        fclose(fp);
        fp=fopen("response.dat","wb");
        if(fp==NULL)
        {
            printf("\nResponse could not be sent");

        }
        fwrite(response,1,strlen(response),fp);
        fclose(fp);
        send_file(strlen(response));

    case 2005 :
        fread(&customer_id,sizeof(customer_id),1,fp);
        printf("\n\nRequest For Expense Delete with Customer Id : %d",customer_id);
        delete_expense_list(customer_id,response);
        printf("\n\nResponse Sent=%s\n",response);
        fclose(fp);
        fp=fopen("response.dat","wb");
        if(fp==NULL)
        {
            printf("\nResponse could not be sent");

        }
        fwrite(response,1,strlen(response),fp);
        fclose(fp);
        send_file(strlen(response));

      }
}

这部分显示警告..

while(1)
{
    f=fread(&b,sizeof(b),1,fp1);
    if(f==NULL)
    {   break;  }
    if(b.customer_id==customer_id)
    {
        fwrite(&b,sizeof(b),1,fp2);
        flag=1;
    }
}

我不明白为什么在“if condition”上指定了这个警告。

1 个答案:

答案 0 :(得分:-1)

fread()函数返回size_t值,而不是指针,因此您无法将NULL与size_t值进行比较。

然后, fread()并不区分文件结尾和错误,因此您必须调用 feof()来检查文件结尾和< strong> ferror()检查错误。