在此程序中,我需要读取文件并进行一些计算。问题是带有注释的行未运行。事件printf。所有其他if条件也可以完美运行,但不是第一个if条件及其上方的printf。无论我在标记行之间写什么,都行不通。有解决方案吗?
#include<stdio.h>
#include<stdbool.h>
#include<stdlib.h>
#include<string.h>
int main(void){
double total=0,max_sale=0,min_sale=0,sale,Southern=0,Western=0,Uva=0,North=0,Eastern=0,Central=0,NorthWest=0,NorthC=0,Sabaragamu=0;
int sales_n=0;
char ID_no[15],max_ID[15],min_ID[15];
char E_name[20],E_tp[10],address[100],province[10],temp[100],name2[20],province2[10];
bool flag = true,flag2,exsist=false;
FILE * dataP;
FILE * saleP;
saleP = fopen("sales.txt","r");
if(saleP==NULL){
printf("Error: Unable to open file\n");
exit(1);
}
while(true){
dataP = fopen("employee_data.dat","r");
if(dataP==NULL){
printf("Error: Unable to open file\n");
exit(1);
}
fscanf(saleP," %s %lf",ID_no,&sale);
if(feof(saleP)){
break;
}
/******************************************************************************************************/
while(!feof(dataP)){
fscanf(dataP," %s",temp);
fscanf(dataP," %s",E_name);
strcpy(name2,E_name);
fscanf(dataP," %s",E_tp);
fscanf(dataP," %s",address);
fscanf(dataP," %s",province);
strcpy(province2,strupr(province));
printf("\nnew province %s",province2);
if(!strcmp(temp,ID_no)){
printf("ID no = %s temp = %s\n",ID_no,temp);
printf("%s\n",province2); //form this line
if(!strcmp(province2,"SOUTHERN")){
Southern +=sale;
printf("Passsed\n");
} //to this line doesn't execute
else if(!strcmp(province2,"WESTERN")){
Western +=sale;
}
else if(!strcmp(province2,"CENTRAL")){
Central +=sale;
}
else if(!strcmp(province2,"NORTHWEST")){
NorthWest +=sale;
}
else if(!strcmp(province2,"NORTHCENTRAL")){
NorthC +=sale;
}
else if(!strcmp(province2,"EASTERN")){
Eastern +=sale;
}
else if(!strcmp(province2,"SABARAGAMU")){
Sabaragamu +=sale;
}
else if(!strcmp(province2,"NORTHEN")){
North +=sale;
}
else if(!strcmp(province2,"UVA")){
Uva +=sale;
}
}
}
printf("ID %s sale %.2lf\n",ID_no,sale);
total+=sale;
sales_n++;
if(sales_n==1){
max_sale=sale;
min_sale=sale;
strcpy(max_ID,ID_no);
strcpy(min_ID,ID_no);
}
if(max_sale<sale){
max_sale=sale;
strcpy(max_ID,ID_no);
}
else if(min_sale>sale){
min_sale=sale;
strcpy(min_ID,ID_no);
}
}
//printf("\ntotal is : %.2lf\n max sell is %.2lf\n min sell is %.2lf\n total sales number is %d",total,max_sale,min_sale,sales_n);
//printf("\nmax seller is %s\nMin seller is %s",max_ID,min_ID);
printf("\n ==============================================================\n");
printf(" S H O W I N G S U M M A R R Y");
printf("\n ==============================================================\n");
printf("\n > Total sales : Rs.%.2lf\n",total);
printf("\n > Total sales count : %d\n",sales_n);
printf("\n > Top seller : %s\n",max_ID);
printf("\n > Top sale : Rs.%.2lf\n",max_sale);
printf("\n > Weakest seller : %s\n",min_ID);
printf("\n > Weakest sale : Rs.%.2lf\n",min_sale);
printf("\n\n\n %.2lf\n %.2lf\n %.2lf\n %.2lf\n %.2lf\n %.2lf\n %.2lf\n %.2lf\n %.2lf\n",Southern,Western,Uva,North,Eastern,Central,NorthWest,NorthC,Sabaragamu);
}
其中一个文件可能因为不存在而无法打开。
在代码中添加以下几行:
....
saleP = fopen("sales.txt","r");
if (saleP == NULL) //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
{
printf("can't open file \n");
exit(1);
}
while(true){
dataP = fopen("employee_data.dat","r");
if (dataP == NULL) // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
{
printf("can't open file \n");
exit(1);
}
fscanf(saleP," %s %lf",ID_no,&sale);
....
如果由于某种原因无法打开文件,则fopen
返回NULL
。如果不进行检查,则程序将继续执行并尝试对该NULL文件指针执行其他文件操作,并且对NULL文件指针进行文件操作会导致undefined behaviour,这基本上意味着一切都会发生,主要是奇怪的错误和其他不可预测的行为。