stream!=NULL问题求助

thuzhangga 2015-12-20 02:26:27
我的问题在于:第一次磁盘上没有相关文件的时候,我可以顺利使用save函数存储数据;但是一旦磁盘上有文件,且读取文件并赋值给cafe和myg,save函数再调用就会报错,出现“Expression:(stream!=NULL)”。不知道问题在哪,诚心求助。

#include"stdio.h"
#include"stdlib.h"
#include"string.h"
struct caf//定义结构体caf咖啡店
{
char caf_name[30];
long int caf_id;
char codename;
double size;
int good[20];
}cafe[10];
struct goods//定义结构体goods商品
{
char goods_name[30];
long int goods_id;
float cost;
float price;
int sale;
int amount[10];
struct goods *next;//定义指向goods结构体的指针next,由此构建链表
};
void imputcaf(caf cafe[10]);//各函数定义
struct goods *imputgoods(goods *G);
void readcaf(struct caf cafe[10]);
struct goods *readgoods(struct goods *G);
void save(struct caf cafe[10],struct goods *G);
int len(struct goods *G)
{
struct goods *z;
z=(struct goods *)malloc(sizeof(struct goods));
z=G->next;
int l=1;
while(z->next!=NULL)
{
l++;
z=z->next;
}
return l;
}
void main()//进入主函数
{
printf("***欢迎使用咖啡店管理系统1.0***\n");
struct caf cafe[10];
struct goods *myg=(goods *)malloc(sizeof(goods));;
if(fopen("D://cafe.txt","rb")==NULL)
{
printf("检测到您的计算机的D盘上无相关数据,正在为您进行数据自动输入\n");
imputcaf(cafe);
myg=imputgoods(myg);
save(cafe,myg);
}
else
{
printf("检测到您的计算机的D盘上有相关数据,已为您完成数据自动读入\n");
readcaf(cafe);
myg=readgoods(myg);
}
save(cafe,myg);
}
void imputcaf(struct caf cafe[10])//定义输入函数
{
for(int i=0;i<10;i++)//输入咖啡店数据
{
if(i==0){strcpy_s(cafe[i].caf_name,"Good Luck");}
if(i==1)
strcpy_s(cafe[i].caf_name,"Delicious");
if(i==2)
strcpy_s(cafe[i].caf_name,"Good Taste");
if(i==3)
strcpy_s(cafe[i].caf_name,"Happy Time");
if(i==4)
strcpy_s(cafe[i].caf_name,"Enjoy Life");
if(i==5)
strcpy_s(cafe[i].caf_name,"Romance");
if(i==6)
strcpy_s(cafe[i].caf_name,"Rose");
if(i==7)
strcpy_s(cafe[i].caf_name,"Dream House");
if(i==8)
strcpy_s(cafe[i].caf_name,"Tsinghua");
if(i==9)
strcpy_s(cafe[i].caf_name,"Peking");
cafe[i].caf_id=201600+i;
cafe[i].codename=65+i;
cafe[i].size=70+3*i;
}
printf("咖啡店数据自动输入成功\n");
}
struct goods *imputgoods(goods *G)
{
struct goods *input_temp,*input_cursor;
G=(struct goods *)malloc(sizeof(struct goods));
G->next=NULL;
input_cursor=G;
for(int i=0;i<6;i++)
{
input_temp=(struct goods *)malloc(sizeof(struct goods));
input_temp->next=NULL;
if(i==0){strcpy_s(input_temp->goods_name,"coffee");}
if(i==1){strcpy_s(input_temp->goods_name,"cake");}
if(i==2){strcpy_s(input_temp->goods_name,"cola");}
if(i==3){strcpy_s(input_temp->goods_name,"juice");}
if(i==4){strcpy_s(input_temp->goods_name,"chocolate");}
if(i==5){strcpy_s(input_temp->goods_name,"ice cream");}
input_temp->goods_id=100000+i;
input_temp->cost=10*(i+1)*1.0;
input_temp->price=11*(i+1)*1.0;
for(int j=0;j<10;j++)
{
input_temp->amount[j]=100+i+j;
}
input_temp->sale=10*(90-i);
input_cursor->next=input_temp;
input_cursor=input_cursor->next;
}
printf("商品数据自动输入成功\n");
return G;
}
void readcaf(struct caf cafe[10])
{
FILE *f1;
f1=fopen("D://cafe.txt","rb");
for(int i=0;i<10;i++)
{
fread(&cafe[i],sizeof(struct caf),1,f1);
}
fclose(f1);
}
struct goods *readgoods(struct goods *G){
FILE *f2;
struct goods *rg_cursor;
rg_cursor=G;
f2=fopen("D://goods.txt","rb");
while(true){
struct goods *g=(struct goods *)malloc(sizeof(struct goods));
if(fread(g,sizeof(struct goods),1,f2)!=1) break;
rg_cursor->next=g;
rg_cursor=rg_cursor->next;
}
rg_cursor->next=NULL;
return G;
fclose(f2);
}
void save(struct caf cafe[10],struct goods *G)//定义存储函数save,将各咖啡店的数据存储到磁盘上
{
FILE *save_f1,*save_f2;
struct goods *save_g=(struct goods *)malloc(sizeof(struct goods));
save_g=G->next;
fopen_s(&save_f1,"D://cafe.txt","wb");
fopen_s(&save_f2,"D://goods.txt","wb");
for(int i=0;i<len(G);i++)
{
fwrite(save_g,sizeof(struct goods),1,save_f2);
for(int j=0;j<10;j++)
{
cafe[j].good[i]=save_g->amount[j];
fwrite(&cafe[j],sizeof(struct caf),1,save_f1);
}
save_g=save_g->next;
}
printf("数据存储成功\n");
fclose(save_f1);
fclose(save_f2);
}
...全文
271 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
thuzhangga 2015-12-20
  • 打赏
  • 举报
回复
引用 1 楼 paschen 的回复:
fopen返回值不是表示是否存在这个文件,而是表示是否成功打开,失败的原因可能有很多 其次你在else里已经打开了文件,在readcaf不应该再打开一次,否则在执行readcaf,把上一次打开的先关闭
首先很感谢您的回答。 我按照您说的改了两次。第一次我在main函数里增加一个文件指针*fp,在if里判断是否打开成功,如果成功(即进入else),就关闭*fp,再调用readcaf。然而这不可取。 第二次我没有用fopen来判断是否存在,而是用io.h里的access,但这也行不通(我不知道access在判断文件是否存在的时候会不会打开文件)。代码如下:

#include"stdio.h"
#include"stdlib.h"
#include"string.h"
#include"io.h"
struct caf//定义结构体caf咖啡店
{   
	char caf_name[30];
	long int caf_id;
	char codename;
	double size;
	int good[20];
}cafe[10];
struct goods//定义结构体goods商品
{
	char goods_name[30];
    long int goods_id;
    float cost;
    float price;
    int sale;
	int amount[10];
    struct goods *next;//定义指向goods结构体的指针next,由此构建链表
};
void imputcaf(caf cafe[10]);//各函数定义
struct goods *imputgoods(goods *G);
void readcaf(struct caf cafe[10]);
struct goods *readgoods(struct goods *G);
void save(struct caf cafe[10],struct goods *G);
int len(struct goods *G)
{
	struct goods *z;
	z=(struct goods *)malloc(sizeof(struct goods));
	z=G->next;
	int l=1;
	while(z->next!=NULL)
	{
		l++;
		z=z->next;
	}
	return l;
}
void main()//进入主函数
{
	printf("***欢迎使用咖啡店管理系统1.0***\n");
	struct caf cafe[10];
	struct goods *myg=(goods *)malloc(sizeof(goods));;
	if(access("D://cafe.txt",0)==-1)
	{
		printf("检测到您的计算机的D盘上无相关数据,正在为您进行数据自动输入\n");
		imputcaf(cafe);
	    myg=imputgoods(myg);
	}
	else
	{
		printf("检测到您的计算机的D盘上有相关数据,已为您完成数据自动读入\n");
		readcaf(cafe);
	    myg=readgoods(myg);
	}
      save(cafe,myg);
}
void imputcaf(struct caf cafe[10])//定义输入函数
{
    for(int i=0;i<10;i++)//输入咖啡店数据
    {   
		if(i==0){strcpy_s(cafe[i].caf_name,"Good Luck");}
		if(i==1)
			strcpy_s(cafe[i].caf_name,"Delicious");
		if(i==2)
			strcpy_s(cafe[i].caf_name,"Good Taste");
		if(i==3)
			strcpy_s(cafe[i].caf_name,"Happy Time");
		if(i==4)
			strcpy_s(cafe[i].caf_name,"Enjoy Life");
		if(i==5)
			strcpy_s(cafe[i].caf_name,"Romance");
		if(i==6)
			strcpy_s(cafe[i].caf_name,"Rose");
		if(i==7)
			strcpy_s(cafe[i].caf_name,"Dream House");
		if(i==8)
			strcpy_s(cafe[i].caf_name,"Tsinghua");
		if(i==9)
			strcpy_s(cafe[i].caf_name,"Peking");
		cafe[i].caf_id=201600+i;
	    cafe[i].codename=65+i;
	    cafe[i].size=70+3*i;
	}
	printf("咖啡店数据自动输入成功\n");
}
struct goods *imputgoods(goods *G)
{
	struct goods *input_temp,*input_cursor;
    G=(struct goods *)malloc(sizeof(struct goods));
    G->next=NULL;
	input_cursor=G;
    for(int i=0;i<6;i++)
    {
		input_temp=(struct goods *)malloc(sizeof(struct goods));
		input_temp->next=NULL;
		if(i==0){strcpy_s(input_temp->goods_name,"coffee");}
		if(i==1){strcpy_s(input_temp->goods_name,"cake");}
		if(i==2){strcpy_s(input_temp->goods_name,"cola");}
		if(i==3){strcpy_s(input_temp->goods_name,"juice");}
		if(i==4){strcpy_s(input_temp->goods_name,"chocolate");}
		if(i==5){strcpy_s(input_temp->goods_name,"ice cream");}
		input_temp->goods_id=100000+i;
		input_temp->cost=10*(i+1)*1.0;
		input_temp->price=11*(i+1)*1.0;
		for(int j=0;j<10;j++)
		{
			input_temp->amount[j]=100+i+j;
		}
		input_temp->sale=10*(90-i);
		input_cursor->next=input_temp;
		input_cursor=input_cursor->next;
    }	
	printf("商品数据自动输入成功\n");
	return G;
}
void readcaf(struct caf cafe[10])
{
	FILE *f1;
	f1=fopen("D://cafe.txt","rb");
	for(int i=0;i<10;i++)
	{
		fread(&cafe[i],sizeof(struct caf),1,f1);
	}
	fclose(f1);
}
struct goods *readgoods(struct goods *G){
	FILE *f2;
    struct goods *rg_cursor;
	rg_cursor=G;
    f2=fopen("D://goods.txt","rb");
	while(true){
		struct goods *g=(struct goods *)malloc(sizeof(struct goods));
		if(fread(g,sizeof(struct goods),1,f2)!=1) break;
		rg_cursor->next=g;
		rg_cursor=rg_cursor->next;
	}
	rg_cursor->next=NULL;
	return G;
	fclose(f2);
}
void save(struct caf cafe[10],struct goods *G)//定义存储函数save,将各咖啡店的数据存储到磁盘上
{
	FILE *save_f1,*save_f2;
	struct goods *save_g=(struct goods *)malloc(sizeof(struct goods));
	save_g=G->next;
	fopen_s(&save_f1,"D://cafe.txt","wb");
	fopen_s(&save_f2,"D://goods.txt","wb");
	for(int i=0;i<len(G);i++)
	{
		fwrite(save_g,sizeof(struct goods),1,save_f2);
		for(int j=0;j<10;j++)
		{
			cafe[j].good[i]=save_g->amount[j];
			fwrite(&cafe[j],sizeof(struct caf),1,save_f1);
		}
		save_g=save_g->next;
	}
	printf("数据存储成功\n");
	fclose(save_f1);
	fclose(save_f2);
}
paschen 2015-12-20
  • 打赏
  • 举报
回复
fopen返回值不是表示是否存在这个文件,而是表示是否成功打开,失败的原因可能有很多 其次你在else里已经打开了文件,在readcaf不应该再打开一次,否则在执行readcaf,把上一次打开的先关闭
thuzhangga 2015-12-20
  • 打赏
  • 举报
回复
引用 3 楼 paschen 的回复:
save 检查fopen_s有没打开成功!! 如果你前面打开了文件,不调用fclose,那么后面写文件就可能无法打开成功!! 另外我不知你把fclose(f2); 写在return后面干嘛,估计是这个原因
确实是return之前应先关闭,我的问题完美解决,谢啦~~~分数敬上
paschen 2015-12-20
  • 打赏
  • 举报
回复
save 检查fopen_s有没打开成功!! 如果你前面打开了文件,不调用fclose,那么后面写文件就可能无法打开成功!! 另外我不知你把fclose(f2); 写在return后面干嘛,估计是这个原因

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧